Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

Lisa

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 WHICHC() function?

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...