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!
@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;
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;
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.
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.