Google SAS Search

Add to Google

Wednesday, September 27, 2006

Input Into Numeric

Many times you have a variable in SAS that is character and you want to convert it to numeric. This tends to come up a lot when importing from Excel. Excel shows a number, but SAS reads the column in as a character variable.

It is a bit difficult to *replace* the original character variable with a numeric one, but it is trivial to create a new numeric variable. Just use the input() function.

The syntax is:
numericVar = input(charVar, informat.);

NumericVar is the numeric variable you are hoping to create.
CharVar is the character variable that holds the 'number'.
Informat is a numeric informat that tells SAS how to translate the numeric 'characters' into a useful number. Dates are often used to illustrate this concept:

data _null_;
charDate = '01mar06';
numDate = input(charDate, date7.);

In the above case, date7. tells SAS how to interpret the character string '01mar06'
into a number (in this case, the number of days since Jan 01, 1960).

Of course your character variable can be something as simple as '1234'. In that case this would work:

data _null_;
charVar = '1234';
numvAR = input(charVar, 4.);

People often confuse the input() function with the put() function. I always
remembered by emphasizing the n sound with this little refrain:

Input Into Numeric.

1 comment:

  1. Hi Stephen, nice blog entry!

    Would it be okay with you if we mirror your Dsta Steps blog at my site? You may want to see how Alan Churchill's blog is published at sas2themax with no effort from his part! --

    Thanks, Audi