BookmarkSubscribeRSS Feed
LarissaW
Obsidian | Level 7

Hi guys,

I have a categorical variable which contains multiple observations, for example, "A29.3" "C22.4" "E38.1" and "C43.6"... I want to create a new variable and assign them with the number. If I want "A29.3" "C22.4" "E38.1" all to be 1 in the new column, what should I do?

I tried the code below, but when I use proc freq to check, I think SAS only transfers the last statement to 1 in the new column.

data CE;
set CE;
if final_cause="A29.3" then status=1;

if final_cause="C22.4" then status=1;

if final_cause="E38.1" then status=1;

else status=0;
run;

Any idea on this? Thanks!

 

 

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@LarissaW wrote:

Hi guys,

I have a categorical variable which contains multiple observations, for example, "A29.3" "C22.4" "E38.1" and "C43.6"... I want to create a new variable and assign them with the number. If I want "A29.3" "C22.4" "E38.1" all to be 1 in the new column, what should I do?

I tried the code below, but when I use proc freq to check, I think SAS only transfers the last statement to 1 in the new column.

data CE;
set CE;
if final_cause="A29.3" then status=1;

if final_cause="C22.4" then status=1;

if final_cause="E38.1" then status=1;

else status=0;
run;

Any idea on this? Thanks!

 

 


I'm not sure I grasp the desired output. Saying something does not work does not really indicate what you do want. Please show us the desired output.

 

Also, I note this is generally considered a sub-optimal practice to overwrite your data set CE with a new data set (although I guess there are times when its not so bad). Better would be

 

data ce1;
    set ce;

 

 

--
Paige Miller
Tom
Super User Tom
Super User

You ran three distinct IF/THEN/ELSE blocks.  So only the results of the last one will survive.

 

Either make it one long IF/THEN/ELSE IF.... ELSE series

if final_cause="A29.3" then status=1;
else if final_cause="C22.4" then status=1;
else if final_cause="E38.1" then status=1;
else status=0;

Of just use the IN operator so you only need one IF condition.

if final_cause in ("A29.3" "C22.4" "E38.1") then status=1;
else status=0;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 925 views
  • 1 like
  • 3 in conversation