How to Convert Character to Numeric Variable in SAS

In this article we’re going to deep dive into the most common question from SAS users. I’m sure you also have the same question on how to convert variable values from character to numeric 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 character to numeric.

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 Character to Numeric Variable in SAS

You can use the INPUT() function in SAS to convert a character variable to a numeric variable. 

This function uses the following simple syntax:

				Numeric_variable = input(character_variable, informat.);
			

Example 1:

If you have a simple string of digits (numbers only) then you can use informat 8. Please note this won’t work if you have any character value in the data.

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

			

Example 2:

If you have leading zeros in the data then above code where we have used informat 8. Will remove those leading zeros. In case if you want to keep leading zeros then you need to use following code:

				data new;
   char_var = '0045';
   numeric_var = input(char_var, $4.);
run;

			

Example 3:

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

				data new;
char_var = '12,000,000';
char_var2 = '$6,000,000';
numeric_var = input(char_var,comma10.);
numeric_var = input(char_var2,dollar10.);
run;

			

Example 4: Convert character date to numeric sas date

The following code starts with a character string “15MAR2025”, 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 analysed and manipulated by the date functions:

				data new;
startdate = "15MAR2025";
date_var = input(startdate,date9.);
format date_var date9.;
run;

			

Example 5:

Assume you have the following employee dataset in SAS where employeeID, name , and DOB are character variables and Salary is a numeric variable.

				data employee;
	input employeeID $ 1-4 DOB $ 5-14 Name $ 15-29 Salary 8.;
	datalines;
0001 10041980 John Doe        50000
0002 12061986 Jane Smith      60000
0003 09021991 Bob Johnson     55000
0004 10051995 Emily Davis     65000
0005 10021990 Michael Brown   75000
0006 03111983 Sarah Miller    80000
0007 12051970 David Garcia    90000
0008 18041999 Jessica Wilson  85000
0009 05071987 James Moore    100000
0010 20101990 Melissa Taylor 115000
;
run;
proc print data=employee; run;
			
sas employee data set

We can use the proc contents to see the data type of all the variables present in the employee dataset.

				/*display data type for each variable*/
proc contents data=employee; run;
			
sas employee data set proc content

Let’s perform the following changes:

1. Convert employeeID character variable to numeric variable

Let’s focus on the employeeID variable first and create a new dataset “new_employee” by using following code to convert character variable to numeric variable.

You need to give a new name to your numeric variable. In this case we will create a new variable “numeric_employeeID”.

				numeric_employeeID= input(employeeID, $4.);
     /* OR  - both the expresessions are correct*/
numeric_employeeID= input(employeeID, 8.);
			

2. Convert DOB character variable into numeric variable with date9. Format

				format numeric_DOB date9.;
numeric_DOB= input(DOB, ddmmyy10.);
			

Let’s create a new data set “new_employee” with following formats:

  • numeric_employeeID: Convert character variable “employeeID” to numeric variable and name it as a numeric_employeeID.
  • DOB: Comvert character variable “DOB” to numeric variable “numeric_DOB” but in date9 format
				/*create a new dataset*/
data new_employee;
	format numeric_DOB date9.;
	set employee;
	numeric_employeeID=input(employeeID, $4.);
	numeric_DOB=input(DOB, ddmmyy10.);
	drop employeeID DOB;
run;

/*view new dataset*/
proc print data=new_employee;
run;

/* view the data type of all the variables */
proc print data=new_employee;
run;
			
convert character variables to numeric variables in SAS
convert character to numeric variables in SAS

FAQ

What is INPUT() function in SAS?

You can use the INPUT() function in SAS to convert a character variable to a numeric variable. 

Syntax:

Numeric_variable = input(character_variable, informat.);
How to convert character variable to numeric variable in SAS?

The character variables can be converted into numeric variables using INPUT() function in SAS. 

PS. In SAS a variable can be defined as only one type, so you cannot use the same variable name to convert the values.