How to convert Characters to Numeric in SAS & Vice Versa

• Post author:
• Post category:Base SAS

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`

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   *```

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.

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

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.

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