SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SanderB
Obsidian | Level 7

I’ve been writing macros for many years now, but I’ve run into a problem that I’ve never had before. Apparently, %INCLUDE works differently than I thought. Although I think I have done things like this in the past, I probably remember wrong because it doesn’t seem to work. And I can’t find any documentation explaining this.

 

As an example this piece of code works without problems:

%macro TestMacro;

 %if 1 = 1 %then %do;
    %if 2 = 2 %then %do;
       %put THIS WORKS;
    %end;
 %end;

%mend TestMacro;

%TestMacro;

 

But if you put that piece with nested %IFs in a text file and include it in your code, it doesn’t work anymore:

%macro TestMacro2;

 %include "…some path…\testinclude.txt";

%mend TestMacro2;

%TestMacro2;

 

Depending on the version you’re running you can get two different errors:

  • ERROR: The %IF statement is not valid in open code.
  • ERROR: Nesting of %IF statements in open code is not supported.

 

The error messages suggest that the code in the include file is run in open code, as if it is executed in a separate environment or something. But you “inherit” stuff like macro variables and other macros, so that doesn’t seem to be the case. Is it possible that SAS runs a completely separate syntax check on the code in the include file, which doesn’t include the macro that is around it?

 

I’ve looked for any documentation online but I can’t find anything.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your error message makes perfect sense.  You created a macro that generates ONE statement.  The %INCLUDE statement.  So by the time the %INCLUDE statement runs the macro processor has finished its work so the %IF statement in the included file is being run by SAS, not by the macro processor. 

 

And the actual text in the included has definitely NOT been used in compiling the macro's definition.  Just the %INCLUDE statement itself becomes part of the compiled macro.  Try it with a %INCLUDE file that does work.  Then change the contents of the file after compiling the macro and check which version of the file is used when the macro is called after the contents of the included file has been changed.

filename include temp;
data _null_;
  file include;
  put '%put BEFORE macro compiled.;';
run;
%macro test;
%include include/source2;
%mend test;
%test;
data _null_;
  file include;
  put '%put AFTER macro compiled.;';
run;
%test;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Your error message makes perfect sense.  You created a macro that generates ONE statement.  The %INCLUDE statement.  So by the time the %INCLUDE statement runs the macro processor has finished its work so the %IF statement in the included file is being run by SAS, not by the macro processor. 

 

And the actual text in the included has definitely NOT been used in compiling the macro's definition.  Just the %INCLUDE statement itself becomes part of the compiled macro.  Try it with a %INCLUDE file that does work.  Then change the contents of the file after compiling the macro and check which version of the file is used when the macro is called after the contents of the included file has been changed.

filename include temp;
data _null_;
  file include;
  put '%put BEFORE macro compiled.;';
run;
%macro test;
%include include/source2;
%mend test;
%test;
data _null_;
  file include;
  put '%put AFTER macro compiled.;';
run;
%test;
SanderB
Obsidian | Level 7
Oh D***!!!.... I understand what you're saying. Two experienced SAS programmers were looking at it and did not spot the issue. To be fair, the code they were looking at was way more complex than the example above, but still we should have seen this! Thank you very much! I can relax this evening because now I how to solve the problem tomorrow 😉

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1572 views
  • 2 likes
  • 2 in conversation