Character-to-Numeric Conversion, Using the INPUT Function - SAS Programming

One of the most extremely useful (and poorly understood) SAS System functions is the INPUT function. It takes the form:

variable=INPUT (variable,informat);

The traditional use is to reread a character variable with a numeric format,executing a character-to-numeric conversion.

In this example you have a collection of human temperatures (in degrees F).Instead of entering 98.6, the letter N was used (for Normal).The following program reads the data and converts the N's to 98.6:

Example

DATA TEMPER;
INPUT DUMMY $ @Q;
IF DUMMY='N' THEN TEMP=98.6;
ELSE TEMP=INPUT (DUMMY,5.)?
DROP DUMMY;
DATALINES;
99,7 N 97,9 N N 112.5
RUN;

Since you have a mixture of numeric and character data,you have to read all the values as character data.Otherwise,reading N as a numeric value would result in a missing value for that observation.Let's see what happens when the program reads the first few data values.DUMMY is first assigned a value of 99.7 (remember that this is a character string).Since this is not equal to N, the variable TEMP is set to 99.7 and stored as a numerical value because of the 5.informal.The next value for DUMMY is N.The IF statement is true,and the variable TEMP is set to 98.6 (a numeric value).


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics