Hi. I was wondering if someone could help me with my code. Here it is:
data 6;
set 5;
if Q34 = "White" then Race = 1;
if Q34 = "Black or African American" then Race = 2;
if Q34 = "Asian" then Race = 3;
Else Race = 4;
run;
However, in the proc print, I have noticed it does not work correctly as you can see here:
White should be 1 under race but it comes up as 4, do you know why this is?
Your If conditions need to be mutually exclusive. Try.
if Q34 = "White" then Race = 1;
else if Q34 = "Black or African American" then Race = 2;
else if Q34 = "Asian" then Race = 3;
Else Race = 4;
Btw, 5 and 6 are not valid names for the sas datasets.
Your If conditions need to be mutually exclusive. Try.
if Q34 = "White" then Race = 1;
else if Q34 = "Black or African American" then Race = 2;
else if Q34 = "Asian" then Race = 3;
Else Race = 4;
Btw, 5 and 6 are not valid names for the sas datasets.
@K_Wils15 wrote:
Hi. I was wondering if someone could help me with my code. Here it is:
data 6;
set 5;
if Q34 = "White" then Race = 1;
if Q34 = "Black or African American" then Race = 2;
if Q34 = "Asian" then Race = 3;
Else Race = 4;
run;However, in the proc print, I have noticed it does not work correctly as you can see here:
White should be 1 under race but it comes up as 4, do you know why this is?
The Else as you have written the code only applies to the Q34="Asian". So when Q34="White" the Else is executed instead of the assignment of 3.
@r_behata has the correct approach for the If/then/else structure. I'm just providing some detail as to why your code was wonky.
Both replies are of course correct. In general, you will find that standard parts of SAS that have been successfully used for decades, ALWAYS work properly. If you are not getting the results you expect, it is a user error not a SAS error.
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.