March 2006 Technical Tip – Recoding data in SAS
Continuous data, such as income, is well suited for many forms of statistical analysis, such as the five measures produced by PROC MEANS (count, mean, minimum, maximum, and standard deviation) or as a variable in linear regression. But in some instances, such as in logistic regression, categorical data is preferred. This article illustrates a simple means by which continuous data can be recoded, or grouped, as categorical data.
In the example which follows, student test scores (continuous) are converted to both their letter grade and numeric grade equivalents (categorical). Thus the reader can see how to create categorical data which is either character or numeric.
PROC FORMAT can be used to recode or group numeric or character data. In this example we use numeric data only (student's test score.)
The PUT function outputs a variable using a specified format. So if score has a value of 75 then put(score, NbrToLtr.) outputs the character C. So the statement ltrGrade = put(score, NbrToLtr.); assigns the letter C to the variable ltrGrade.
But the output from PROC FORMAT is always character. So how do we recode or group data as a number? The INPUT function converts a character value to a numeric variable using an INFORMAT. For example, input("2", 1.0) converts the character "2" to the number 2. We can nest the PUT and INPUT functions, so if score has a value of 75 then the statement nbrGrade = input(put(score, NbrToNbr.),1.0); assigns the number 2 to the variable nbrGrade.
The output from PROC PRINT is as follows:
If the input to your PROC FORMAT is character data, then you must use a leading dollar sign on the format name. The following example shows how to recode gender from the letter 'M' to number 0 and from the letter 'F' to number 1:
Reminder: you cannot use a trailing period when defining a format in PROC FORMAT but you must use a trailing period when using the format name in a PUT or INPUT function!
We hope you will consider Caliber Data Training when you are in need of a SAS training provider.