Converting Character Variables to Numeric Variables

This is an example of how to change a character variable, AGE, to numeric.

This program reliably changes character variables to numeric variables. The INPUT function takes two arguments: the name of a character variable and a SAS informat or user-defined informat for reading the data.

newage = input(age,3.0);
drop age;
rename newage=age;

Below are a few examples of conversions using INPUT function (character variable “oldvar” to numeric variable “newvar“):

oldvar input function newvar
32000 newvar=input(oldvar, 5.); 32000
32000 newvar=input(oldvar, 5.2); 320.00
32,000 newvar=input(oldvar,comma6.); 32000

NOTE: If a character variable only includes numbers, it can be automatically converted to a numeric variable by using it in a numeric context. For example:

newvar = oldvar + 0 ;
newvar = oldvar*1 ;

Quick Links