- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content