BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kfluegge
Fluorite | Level 6

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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.

 



 

View solution in original post

8 REPLIES 8
Reeza
Super User
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.

 



 

PeterClemmensen
Tourmaline | Level 20

Drop the percent signs.

novinosrin
Tourmaline | Level 20

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;
Reeza
Super User
You should use @novinosrin solution. It's slightly simpler and one less step which always makes things faster.
novinosrin
Tourmaline | Level 20

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

 

 

 

Reeza
Super User

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 🙂

 

 

novinosrin
Tourmaline | Level 20

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 🙂

 

 

 

kfluegge
Fluorite | Level 6

Thanks to all. I am somewhat new to SAS. I will use the correction for now and consider 's suggestion for future.

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 8 replies
  • 1453 views
  • 7 likes
  • 4 in conversation