In this article we’re going to deep dive into the most common question from SAS users. I’m sure you’re also wondering how to convert variable values from numeric to character in SAS.
On multiple occasions you do need to perform the data value conversion especially when you’re reading data from different sources. The multiple sources have multiple types of source data and to combine together or perform data management operations you have to convert char to integer or integer to char in SAS.
With the multiple examples I’m going to address all the possible combinations where you may need to convert values or SAS variables from numeric to character.
The INPUT and PUT functions convert values for a variable from character to numeric, and from numeric to character. In SAS a variable can be defined as only one type, so you cannot use the same variable name to convert the values.
Convert Numeric Variable to Character Variable in SAS
You can use the PUT() function in SAS to convert a numeric variable to a character variable.
This function uses the following simple syntax:
character_variable = put(numeric_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.
Example 1:
You have numeric variable (num_var) and want to create a new character variable with name “char_var” by converting numeric variable value into character variable using put() function.
data new;
num_var=123456;
char_var=put(num_var, 6.);
run;
proc contents data=new; run;
Example 2:
Suppose we have an “employee” dataset with two numeric variables, employeeID and Salary. The task is to create a new dataset “new_employee” with character variables (char_Salary, char_employeeID) using numeric variables (Salary, employeeID) from existing “employee” dataset.
/* create dataset */
data employee;
input employeeID Salary;
datalines;
1 50000
2 60000
3 55000
4 65000
5 75000
6 80000
7 90000
8 85000
9 100000
10 115000
;
run;
/* check the data type of variables */
proc contents data=employee;
run;
/* convert numeric variables into character variables */
data new_employee;
set employee;
char_employeeID=put(employeeID, 8.);
char_Salary=put(Salary, 8.);
drop employeeID Salary;
run;
/* check the data type of variables */
proc contents data=new_employee;
run;
This is the easiest way to convert numeric value or numeric variable into character variable in SAS.
FAQ
The put() function is used to convert numeric value or variable into character variable in SAS.
Syntax:
character_variable = put(numeric_variable, format.);
The put() function is used to convert character value or variable into numeric variable in SAS.
Syntax:
Numeric_variable = input(char_variable, informat.);
You can use PUT() function in SAS to convert numeric variable into character variable.
Example:
data new;
num_var=123456;
char_var=put(num_var, 6.);
run;