Convert Variable Values From Character To Numeric Or From Numeric To Character

The INPUT and PUT functions convert values for a variable from character to numeric, and from numeric to character. A variable can be defined as only one type, so you cannot use the same variable name to convert the values. When you use the functions below, you create a new variable with the values converted. If you want the end result to be the original variable name, you need to drop the original variable and rename the new variable to the old name.

Note: The examples listed below use hardcoded values for simplicity. See the Full Code tab for examples of how to perform these actions when reading a data set.

Convert character to numeric
To convert character values to numeric values, use the INPUT function.

new_variable = input(original_variable, informat.);

The informat tells SAS how to interpret the data in the original character variable.

For example, if you have a simple string of digits like , you can use the basic numeric informat w.d:

data new;
char_var = ‘ ‘;
numeric_var = input(char_var, 8.);
run;

If you want your resulting data set to use the original variable name as a different type, you need to drop the original variable and rename the new variable to the old name:

data new;
orig = ‘ ‘;
new = input(orig, 8.);
drop orig;
rename new=orig;
run;

If you want to display the leading zeros in your numeric value as they were shown in your character string, you need to use the basic numeric informat w.d and then apply the format Zw.d to the numeric variable.

char_var = ‘0012’;
numeric_var = input(char_var, $4.);
format numeric_var z4.;

If your string contains nondigits such as commas or dollar signs, you need to use the correct informat:

char_var = ‘12,000,000’;
numeric_var = input(char_var,comma10.);

char_var = ‘$6,000,000’;
numeric_var = input(char_var,dollar10.);

As with all numeric values, the data is saved with only numbers in the numeric_var variable. If you were to display it, you would see . You can use the FORMAT statement to display the new variable in the desired format.

format numeric_var dollar10.;

The informat and format documentation lists the choices by type, so you can see which one matches your data. If you choose an informat that does not match your data or have some values that do not match, using the INPUT function results in a missing value. A NOTE like the following is also displayed in the SAS log:

NOTE: Invalid argument to function INPUT at line 725 column 8.

If you know that you have some invalid data in the original variable and do not want every NOTE listed in your SAS log, you can suppress the printing of these notes by using the ? or ?? modifier with the informat:

numeric_var = input(char_var, ? 8.);

See the INPUT function documentation for more information.

You can use the CONTENTS procedure to ensure that the new variable name is the type that you expect.

proc contents data=data_set_name;
run;

Using the INPUT function to convert a character date to a SAS date
A SAS date is a numeric value with which you can use date functions and calculations. A SAS date might be formatted so that it contains characters in the display, but it is always stored as a numeric variable.

For example, suppose you receive a data set that contains a variable named Startdate that is displayed in PROC PRINT output as 12JUL2016.

Is Startdate a character string “12JUL2016” or is it a numeric variable with a format applied? You can use PROC CONTENTS to verify the variable type and to see whether a format has been applied to it. If it is a character variable, you can convert it to a SAS date so that you can take advantage of the SAS date functions. Look at the date informats to determine which informat matches your values. In this case, DATE9. reads date values in the form ddmmmyy, which matches 12JUL2016.

The following code starts with a character string “12JUL2016”, creates a SAS date, and then formats it with the DATE9. format. The end result is a SAS date that looks the same as the original variable, but can be analyzed and manipulated by the date functions:

startdate = “12JUL2016”;
date_var = input(startdate,date9.);
format date_var date9.;

The same methodology applies to time and datetime values. Click the Full Code tab to see examples of these.

Convert numeric values to character
To convert numeric values to character, use the PUT function:

new_variable = put(original_variable, format.);

The format tells SAS what format to apply to the value in the original variable. The format must be of the same type as the original variable. For example, if you are using the PUT function to convert a numeric value to character, the format must be a numeric format.

For example, if you have the numeric value , you can use the basic numeric format w.d:

num_var = ;
char_var = put(num_var,6.);

When you start with a numeric value, the resulting string is right-aligned by default. This means that if you use a format in the PUT function that is longer than the value being converted, the resulting character value is right aligned. If you want it to be left-aligned, you can use the -L specification with the PUT function:

char_var = put(num_var, 10. -L);

Putting it all together
You have seen how to convert numeric values to character and character values to numeric. Both of these steps are needed to convert a numeric value that looks like mmddyyyy but is not a SAS date, to a SAS date.

Start with a numeric variable with a value of as an example. This looks like a SAS date, but when you use PROC CONTENTS, you see that there is no format applied, so it is actually the integer value . You want it to be a SAS date with a format that displays the value as . How do you convert a numeric value to a SAS date?

Click the Full Code tab to see the solution for this question and many more examples.

For even more information about converting values, see the following SAS tutorial video:

>

These sample files and code examples are provided by SAS Institute Inc. “as is” without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.