Hello, I would like to write code that generates a variable equal to 1 if another variable equals a value in a macro list. I am using SAS EG 7.1 on 64-bit Win 7. The code I have is below:
%let oldPN_ICD9 = '4800', '4801'; data &ADMISSION; set &ADMISSION; %if DIAG1 = oldPN_ICD9 %then PN_diag = 1; %else PN_diag = 0; run;
The error I receive is below:
ERROR: The %IF statement is not valid in open code.
data &ADMISSION; set &ADMISSION; *having same name is a bad idea. Harder to debug; if DIAG1 IN (&oldPN_ICD9) then PN_diag = 1; else PN_diag = 0; run;
See modifications above.
You need to have code that generates a variable so this should work.
Note that the use of ADMISSION in DATA and SET statements is not recommended. Its an easy way to get errors that are harder to debug.
@kfluegge wrote:
Hello, I would like to write code that generates a variable equal to 1 if another variable equals a value in a macro list. I am using SAS EG 7.1 on 64-bit Win 7. The code I have is below:
%let oldPN_ICD9 = '4800', '4801'; data &ADMISSION; set &ADMISSION; %if DIAG1 = oldPN_ICD9 %then PN_diag = 1; %else PN_diag = 0; run;The error I receive is below:
ERROR: The %IF statement is not valid in open code.
data &ADMISSION; set &ADMISSION; *having same name is a bad idea. Harder to debug; if DIAG1 IN (&oldPN_ICD9) then PN_diag = 1; else PN_diag = 0; run;
See modifications above.
You need to have code that generates a variable so this should work.
Note that the use of ADMISSION in DATA and SET statements is not recommended. Its an easy way to get errors that are harder to debug.
@kfluegge wrote:
Hello, I would like to write code that generates a variable equal to 1 if another variable equals a value in a macro list. I am using SAS EG 7.1 on 64-bit Win 7. The code I have is below:
%let oldPN_ICD9 = '4800', '4801'; data &ADMISSION; set &ADMISSION; %if DIAG1 = oldPN_ICD9 %then PN_diag = 1; %else PN_diag = 0; run;The error I receive is below:
ERROR: The %IF statement is not valid in open code.
Drop the percent signs.
like this?
%let oldPN_ICD9 = '4800', '4801';
data have;
input var $;
cards;
4800
4801
4802
;
data want;
set have;
PN_diag =var in (&oldPN_ICD9);
run;
Thanks @Reeza, but to be fair, we have to take into consideration OP may not "yet" be aware of boolean expressions. had it been 5 years ago when i started really using SAS, I would have chose the explicit if then.
So better to be safe than sorry in my opinion
1. start with if then
2. then ifn
PN_diag =ifn(var in (&oldPN_ICD9),1,0);
3. boolean
Not trying to judge OP, just my 2 cents thought
Nope, I fully agree. I usually tailor my solutions to the OP's level as much as I can.
In this case, it makes sense to show how to fix the code (my solution) and then an 'optimal' solution which would be yours.
IFN() is another option that I constantly forget about, it was introduced after I stopped programming daily 🙂
Absolutely. If i recall what i heard and if it's accurate, I vaguely remember there is gonna be a new category for newbie/learners or something along those lines . That will make us regulars to have an idea of OPs background as we cater our solution to their needs. And then as OPs gradually move to next level, the solutions will also change. Fun all the way 🙂
Thanks to all. I am somewhat new to SAS. I will use the correction for now and consider novinosrin's suggestion for future.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.