BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SAS_Novice22
Quartz | Level 8

Hi all,

 

I am a new user to SAS and I am really struggling with some very basic commands. I have a dataset where I have 5 variables of race (e.g., asian, black, white, pacific etc.) and teach row represents a subject with an 'X' marking the category they fall into.

SAS_Novice22_1-1654542282342.png

I would like to create a single new variable titled 'race' (which I have successfully done - yay me) and then re-categorize the 5 race variables into my new variable accordingly. Anything missing entries would be coded as ".".

Here is a copy of my code:

SAS_Novice22_0-1654542117531.png

I am not getting any error messages per se but it is not recoding from the original variables to the new variable the way I am intending.

 SAS_Novice22_2-1654542323517.png

Any feedback would be greatly appreciated.

 

Thank you!
T.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You need ELSE before the last 4 IF statements.

--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

You need ELSE before the last 4 IF statements.

--
Paige Miller
SAS_Novice22
Quartz | Level 8
Hi Paige,
Amazing, thank you, that worked.
Have a great day.
T.
Reeza
Super User

You have 4 IF statements and then an IF/ELSE statement. The ELSE in that last statement overwrites any of your first 4 IFs.

 

You should have ELSE IF instead of IF in the 2nd-4th series of statements.

 

Please post your code as text, makes it much easier to correct any mistakes without having to re-type out all the code.

SAS_Novice22
Quartz | Level 8

Thank you very helpful. That fixed this issue. Also thank you for the posting pointers, that was my first post. I will know for next time
Thank you!
T.

Shmuel
Garnet | Level 18

In your code, whenevr other  ne 'x' you will get race = .;

You should add else after each if statement:

 

data want;
 set have;
      if asian = 'X' then race = 1; else
      if balch = 'X' then race = 2; else
     ...
    if other = 'X' then race = 5; else race = .;
run;
Tom
Super User Tom
Super User

Or better you could add the else BEFORE the statements where humans are more likely to notice it and makes each line only contain one statement instead of one and half statements.

ballardw
Super User

Another way to examine multiple variables for the same value is the WHICHC function for Character values or WHICHN for Numeric values.

 

Note here is also a way to provide an example data set:

data example;
   input (asian black pacific white other) ($);
datalines;
x . . . .
. x . . .
. . x . .
. . . x .
. . . . x
. . . . .
;

data want;
   set example;
   race = whichc('x',asian, black, pacific, white, other);
   if race = 0 then race=.;
run;

Whichc, or Whichn, return the position position number of the value if found or 0 otherwise.
Since your problem is related to the sequence of position this function does most of the work.

 

Great big caveat: Nothing in your code provides for multiple race codes selected and a very large number of data systems that collect race will allow multiple answers. If that is possible in your data you likely need to address that potential issue. Quite possibly with a bit more work depending on values and any other rules for your use.

 

You can test for that with code like:

Proc freq data=demo;
   tables asian* black* pacific* white* other/ list missing;
run;

If any of the output lines have more than one variable with an X you need to address that.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 652 views
  • 3 likes
  • 6 in conversation