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;

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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