BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
K_Wils15
Obsidian | Level 7

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11

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.

View solution in original post

6 REPLIES 6
r_behata
Barite | Level 11

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
Obsidian | Level 7
Thank you! Thanks for catching that, I just deleted the first part of the name because I named it after the survey. Thank you.
ballardw
Super User

@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: 

K_Wils15_0-1618435940569.png

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.

WarrenKuhfeld
Rhodochrosite | Level 12

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.

K_Wils15
Obsidian | Level 7
Got it! Thanks Warren!
K_Wils15
Obsidian | Level 7
Thank you for explaining!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 1530 views
  • 1 like
  • 4 in conversation