Variable Conversions

How to convert Characters to Numeric in SAS & Vice Versa

  • Post author:
  • Post category:Base SAS
  • Post comments:2 Comments
  • Reading time:7 mins read

Converting variables in SAS entails transforming numeric variables to character variables or character variables to numeric variables.

PUT() function is used to convert

  • a character variable to another character variable.
  • convert numeric to character

Note – 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, the 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 generally converts 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 suggestions regarding other tips or tricks to add, suggest us in 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

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

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

This Post Has 2 Comments

  1. blog3006

    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.

    1. Subhro

      Thank You!!