Variable conversions in SAS4 min read

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;
FunctionInput Data TypeInput ValueReturned TypeReturned Value
PUT(name,$10.)char, char format‘John’character‘John’
PUT(age, 4.);num, num format25character’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;
FunctionInput Data TypeInput 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 the 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!

2 thoughts on “Variable conversions in SAS<span class="wtr-time-wrap after-title"><span class="wtr-time-number">4</span> min read</span>”

  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 Comment

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

Share via
Copy link