I am trying to complete the following.
Variable Letter has three values (a, b, c). I would like to create a variable Letter_2 with values corresponding to the values of Letter, namely (1, 2, 3).
I know I can do this using three IF Then statements.
if Letter='a' then Letter_2='1'; if Letter='b' then Letter_2='2'; if Letter='c' then Letter_2='3';
Suppose I have 15 values for the variable Letter, and 15 corresponding values for the replacement. Is there a way to do it efficiently without typing the same If Then statement 15 times?
I am new to SAS. Any clue will be appreciated.
Looks like an application for a FORMAT.
First define the format.
proc format ; value $lookup 'a'='1' 'b'='2' 'c'='3' ; run;
Then use it to re-code your variable.
data want; set have; letter2 = put(letter,$lookup.); run;
Or perhaps you could use two temporary arrays and the
data have; input letter $10. ; cards; car apple box ;;;; data want ; set have ; array from (3) $10 _temporary_ ('apple','box','car'); array to (3) $10 _temporary_ ('first','second','third'); if whichc(letter,of from(*)) then letter_2 = to(whichc(letter,of from(*))) ; run;