BookmarkSubscribeRSS Feed
LarissaW
Fluorite | Level 6

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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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