Variable conversions in SAS

Variable conversions in SAS refer as to convert numeric to character variables or character to numeric variables.

PUT() function is used to convert

  • a character variable to another character variable.
  • convert numeric to character. 
  • converts character variable with a user-defined format to another character variable.

Remember –

  • The source format must match the source variable type in PUT()
  • The result of PUT() is always a character variable.
data _null_;
name = "John";
Age = 25;
x=PUT(name, $10.);
y=PUT(age, 4.);
put _all_;
run;
Function Input Data Type Input Value Returned Type Returned Value
PUT(name,$10.) char, char format ‘John’ character ‘John’
PUT(age, 4.); num, num format 25 character ’25’

The INPUT() function is used to convert 

  • Character variable with a numeric value and informat to a numeric variable.
  • converts character variable with a numeric value and informat to a character variable.
  • converts character variable with a numeric value and informat to a numeric variable.

Remember –

  • INPUT() converts a character to numeric and the result is the variable with the same informat
  • INPUT() can create a character or numeric variables based on the informat.
  • The source variable type for INPUT() must always be character variables
data _null;
name = "John";
Age = '25';
doj= '10/05/2007';
Salary='30,000';
w=INPUT(age, 4.);
x= INPUT(age, $4.);
y=input(doj,ddmmyy10.);
z=INPUT(Salary,comma7.);
format y ddmmyy10. z comma7.;
put _all_;
run;
Function Input Data Type Input Value

Returned Type

Returned Value

INPUT(age, 4.);

char, char

’25’ num, num informat

25

INPUT(age, $4.);

char.char ’25’

char,char informat

’25’

INPUT(doj,ddmmyy10.);

char,char

’10/05/2007′

numeric date,date informat

17296

INPUT(Salary,comma7.);

char,char

‘30,000’

numeric, numeric informat

30000

Automatic variable conversions in SAS

SAS variables conversions are automatically done in some situations where a numeric variable is used in a character expression or when a character variable is used in a numeric expression and writes a NOTE in the LOG.

data _null_;
a='5';
b=2;
c=a+b;
put c=;
run;
NOTE: Character values have been converted to numeric values 
at the places given by: (Line):(Column). 76:3
c=7

In the above example, a character variable ‘a’ is used in the numeric expression, therefore SAS has converted the variable to numeric before evaluating the expression. (a+b).

If you have a special character such as “,” to the variable, the automatic conversion fails and an ERROR message is written to the LOG.

NOTE: Character values have been converted to numeric values at the 
places given by: (Line):(Column).76:3
NOTE: Invalid numeric data, a='5,2' , at line 76 column 3.
c=.a=5,2 b=2 c=. ERROR=1 N=1

Therefore, it is a good programming practice to manually convert the variable to its required type.

You can use the PUT and INPUT functions to directly convert from numeric to character and character to numeric.

Additionally, you can use a format with these functions. The PUT function is used to convert from numeric to character and the INPUT function is used to convert from character to numeric.

Character to Numeric variable conversions in SAS

Character variables are converted to a numeric format. A numeric infomat causes the INPUT function to return a numeric value. The selection of the informat depends on the form of the character date.

If you have any special character in the variable, you can use the applicable informat during the conversion as in the example below.

data _null_;
a='5,2';
b=2;
c=input(a,comma.)+b;
put c=;
run;

Convert Numeric to Character and Left Alignment

The PUT function is generally used to convert a numeric value to a character. Since a numeric format is used, the resulting string is right-justified. You can use the LEFT function to left justify the string.

data _null_;
a=10023;
b="*"||put(a,8.)||"*";
put b=;
c="*"||left(put(a,8.))||"*";
put c=;
run;
b=*   10023*
c=*10023   *

Read: SAS date formats: How to display dates correctly?

Read: How to add leading zeros in SAS?

Key Takeaway

So, these are the different functions to convert variables in SAS.

Moreover, if you have any other suggestion regarding other tips or tricks to add then suggest us below the comment section. We would really take those list in our further blog post.
Thanks for reading!

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

2 thoughts on “Variable conversions in SAS”

  1. We stumbled over here different web page and thought I might check things out.
    I like what I see so now i am following you. Look forward to exploring your web
    page again.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap
%d bloggers like this: