DATA Step, Macro, Functions and more

% If statement not valid in open code

Accepted Solution Solved
Reply
Regular Contributor
Posts: 164
Accepted Solution

% If statement not valid in open code

Hello

I'm editing a SAS program I have to include new criteria.  I have an instance where only some of the diagnoses will be for some diagnosis types and the rest for others and wish to set the code accordingly but get the error message above.

In the report I have:

%let dxlist = 'I099','I255','I420','I425','I426','I427','I428','I429','I43','I50','P290',

'F00','F01','F02','F03','F051','G30','G311';

%if &dxlist in 'I420' 'I425' 'I426' then %then %do:

     %let dxtypelista='1' 'W' 'X' 'Y' '3';

Then I use it in the program like:

do i=1 to 25 while (diagcde{i} ne ' ');

do j=1 to 25 while (diagcde{j} ne ' ');

if flag_type6 ne 1 and dx_type(i) in (&dxtypelista) and diagcde(i) in: (&dxlist) then do;

if diagcde(i)=diagcde(j) and dx_type(j) in (&dxtypelistb)

      then diagcde(i)=" ";

end;

The error message is indicating that "%if statement is not valid in open code" nor is the %else but what does this mean and how can I edit above to work? 

Thanks for any and all assistance.


Accepted Solutions
Solution
‎03-09-2012 05:03 PM
Regular Contributor
Posts: 164

% If statement not valid in open code

Hi

I should have advised that I was using version 9.1 where the IN statement in macros was deactivated (which I only found out while searching the web to fix my code).  So I had to do the following:

%macro test;

%if &dxlist eq I43 or &dxlist eq F00 or &dxlist eq F02 %then

%let dxtypelista= '1' 'W' 'X' 'Y' '3';

%else %let dxtypelista= '1' 'W' 'X' 'Y';

%mend test;

This is rather bulky but until I install 9.3 it will have to do. Thanks Reeza and Haikuo for your assistance. 

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

% If statement not valid in open code

%macro test;

%if &dxlist in 'I420' 'I425' 'I426' then %then %do:

     %let dxtypelista='1' 'W' 'X' 'Y' '3';

%mend;

%test

Open code means anywhere out of the box of %macro xxx;      to %mend;

Haikuo

Regular Contributor
Posts: 164

% If statement not valid in open code

Hi

Wow, thanks for the super speedy reply!

Sorry, but I should have explained that I am very new to macros so I'm not understanding what you've provided....can you please explain?

Thanks so much.

Respected Advisor
Posts: 3,156

Re: % If statement not valid in open code

Many macro statements can NOT be used out of macro definition. So you will have to put all of these macro statements inside the macro definition:

%macro test; /*this is the beginning of macro definition, the name of macro is 'test'*/

blah;

blah ;

%mend; /*this is the end of macro definition*/

/*then if you call it when you decide to excute it*/

%test

Therefore you need to put

%if &dxlist in 'I420' 'I425' 'I426' then %then %do:

        %let dxtypelista='1' 'W' 'X' 'Y' '3';

%end;

inside your macro definition. BTW, in this case, you need %end to complete the %do loop.

HTH,

Haikuo

Regular Contributor
Posts: 164

% If statement not valid in open code

Super, thanks so much.  I apologize but in my example, I didn't include the "else" statement for the macro.  Based on your response, I currently have it written as:

%macro test;

%if &dxlist in 'I420' 'I425' 'I426' then %then %do;

%let dxtypelista='1' 'W' 'X' 'Y' '3';

%else %let dxtypelista='1' 'W' 'X' 'Y';

%mend;

%test;

But I get the error message "there is no matching %if statement for the %else.  A dummy macro will be compiled."  What should it be?  Thanks. 

Respected Advisor
Posts: 3,156

% If statement not valid in open code

%macro test;

%if &dxlist in 'I420' 'I425' 'I426' then %then

%let dxtypelista='1' 'W' 'X' 'Y' '3';

%else %let dxtypelista='1' 'W' 'X' 'Y';

%mend;

%test;

I have removed %do in your code, it is not necessary in your context. if you use it, you need to add %end to complete the loop,

%macro test;

%if &dxlist in 'I420' 'I425' 'I426' then %then %do;

%let dxtypelista='1' 'W' 'X' 'Y' '3';

%end;

%else %let dxtypelista='1' 'W' 'X' 'Y';

%mend;

%test;

Regular Contributor
Posts: 164

% If statement not valid in open code

Hi

Sorry but I'm confused:  you indicated you took the "do" statement out so I assumed you wanted me to use:

%macro test;

%if &dxlist in 'I420' 'I425' 'I426' then %then

%let dxtypelista='1' 'W' 'X' 'Y' '3';

%else %let dxtypelista='1' 'W' 'X' 'Y';

%mend;

 

%test;

Doing that produces the error " A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &dxlist in 'I420' 'I425' 'I426'".  Please note that the list is actually longer than the 3 codes used in the example so could moving the next line without a comma or anything be an issue? 

Thanks so much for helping me. 

Respected Advisor
Posts: 3,156

% If statement not valid in open code

%macro test;

%if &dxlist in 'I420' 'I425' 'I426'  %then

%let dxtypelista='1' 'W' 'X' 'Y' '3';

%else %let dxtypelista='1' 'W' 'X' 'Y';

%mend;

%test;

Reeza is right. you have a 'then' , which does not belong to macro, therefore need to removed. Sorry I overlooked that.

Super User
Posts: 19,771

% If statement not valid in open code

You both have then %then in the first line.

Is that something specific to Macro code?

Also verify that for in you have minoperator options set appropriately for your code.

OT it looks a possible lookup based on diag codes so you might want to set up a table that you can query from rather than hardcode the macro variables but programmers choice Smiley Happy.

Regular Contributor
Posts: 164

% If statement not valid in open code

Hi Reeza

Thanks for responding.  Please note that taking out the %then causes the error of where is %then so it must be required.

Also, the reason I've included the diagnosis codes for look up is because a diagnosis could be present on the chart more than once and I'm only interested in comorbid conditions.  So I have to apply the filter of certain diagnoses not both types of 1, w, x, y and M or type 2.  So the program throws out all of the scenarios as described and then I'm only evaluating based on what remains and can say if  code in ('I420' 'I425' 'I426') then chf=1.

Super User
Posts: 19,771

% If statement not valid in open code

You need the %then but not the 'then' prior to that.

In macro code most of the if/then/else logic you add a % in front of to make it work, ie %if, %then.

I don't understand this line still though:

&dxlist in 'I420' 'I425' 'I426'

&dxlist is a set of codes and the in is a set of codes, I'm not sure how that will evaluate and I can't see it ever evaluating as true

Regular Contributor
Posts: 164

% If statement not valid in open code

HI Reeza

Thanks for continuing to help!  What I'm trying to do with the &dxlist in 'I420' 'I425' 'I426' is that if the list of codes being reviewed in the code below are from this list of diagnoses then dxtypelista needs to include type 3 but if not then don't look for where type 3. 

do i=1 to 25 while (diagcde{i} ne ' ');

do j=1 to 25 while (diagcde{j} ne ' ');

if flag_type6 ne 1 and dx_type(i) in (&dxtypelista) and diagcde(i) in: (&dxlist) then do;

if diagcde(i)=diagcde(j) and dx_type(j) in (&dxtypelistb)

      then diagcde(i)=" ";

end;

How would it make more sense to do?  Thanks.

Solution
‎03-09-2012 05:03 PM
Regular Contributor
Posts: 164

% If statement not valid in open code

Hi

I should have advised that I was using version 9.1 where the IN statement in macros was deactivated (which I only found out while searching the web to fix my code).  So I had to do the following:

%macro test;

%if &dxlist eq I43 or &dxlist eq F00 or &dxlist eq F02 %then

%let dxtypelista= '1' 'W' 'X' 'Y' '3';

%else %let dxtypelista= '1' 'W' 'X' 'Y';

%mend test;

This is rather bulky but until I install 9.3 it will have to do. Thanks Reeza and Haikuo for your assistance. 

Super User
Posts: 19,771

% If statement not valid in open code

Regular Contributor
Posts: 164

% If statement not valid in open code

Hi

Maybe I was bug eyed from working on this for so long but I thought my version worked.  Anyway, when I ran it again it wasn't working but in looking at your suggestion it isn't working either and produced the error message "

Macro keyword IF appears as text.  A semicolon or other delimiter may be missing." 

I'm stumped on this so any assistance greatly appreciated.  Thanks.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 16799 views
  • 3 likes
  • 4 in conversation