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.
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:
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.
Any feedback would be greatly appreciated.
Thank you!
T.
You need ELSE before the last 4 IF statements.
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.
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.
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;
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.
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.