Variable conversions in SAS

Variable conversions in SAS refer to converting 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

Remember –

  • The source format must match the source variable type in PUT(). The result of PUT() is always a character variable.

Put Function Example:

data _null_; 
name = "John"; 
Age = 25; 
x=PUT(name, $10.); 
y=PUT(age, 4.); 
put _all_; 
run;
name=John Age=25 x=John y=25 _ERROR_=0 _N_=1

Input Function Example

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;
name=John Age=25 doj=10/05/2007 Salary=30,000 w=25 x=25 y=10/05/2007 z=30,000 _ERROR_=0 _N_=1
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, manually converting the variable to its required type is a good programming practice.

You can directly use the PUT and INPUT functions to 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 suggestions regarding other tips or tricks to add, then suggest us below the comment section. We will take those lists 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.