Recoding Character Variables to Numeric Variables in SAS


This is a sample code to RECODE multiple character variables into numeric variables i.e., assigning numeric values to each unique category of your character variable.

All you need to do is list all the character variables that you want to recode in the %let k = statement.

The problem with this approach is that you won’t be able to control which category of your character variables gets what number. The numeric values will be assigned in ascending order (starting with 1) and in alphabetical order of the categories of your character variable.


DATA complete;
INPUT dishchdisp $ EDDISP $ DISSTATUS $;
burn burn death
death burn burn
burn death burn

%LET k = dishchdisp EDDISP DISSTATUS;
%LET i = 1;

%MACRO mai;
%DO %until(%scan(&k,&i)=);
%LET j = %scan(&k,&i);

PROC SORT DATA = complete;
BY &j;

DATA complete;
SET complete;
BY &j;
IF first.&j THEN &j.n + 1;

%LET i = %EVAL(&i+1);
%MEND mai;