<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic %include results in open code? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/include-results-in-open-code/m-p/803594#M316430</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example this piece of code works without problems:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;%macro TestMacro;

&amp;nbsp;%if 1 = 1 %then %do;
&amp;nbsp;&amp;nbsp;&amp;nbsp; %if 2 = 2 %then %do;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %put THIS WORKS;
&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;
&amp;nbsp;%end;

%mend TestMacro;

%TestMacro;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But if you put that piece with nested %IFs in a text file and include it in your code, it doesn’t work anymore:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;%macro TestMacro2;

&amp;nbsp;%include "…some path…\testinclude.txt";

%mend TestMacro2;

%TestMacro2;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Depending on the version you’re running you can get two different errors:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;ERROR: The %IF statement is not valid in open code.&lt;/LI&gt;&lt;LI&gt;ERROR: Nesting of %IF statements in open code is not supported.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I’ve looked for any documentation online but I can’t find anything.&lt;/P&gt;</description>
    <pubDate>Wed, 23 Mar 2022 16:05:15 GMT</pubDate>
    <dc:creator>SanderB</dc:creator>
    <dc:date>2022-03-23T16:05:15Z</dc:date>
    <item>
      <title>%include results in open code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/include-results-in-open-code/m-p/803594#M316430</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example this piece of code works without problems:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;%macro TestMacro;

&amp;nbsp;%if 1 = 1 %then %do;
&amp;nbsp;&amp;nbsp;&amp;nbsp; %if 2 = 2 %then %do;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %put THIS WORKS;
&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;
&amp;nbsp;%end;

%mend TestMacro;

%TestMacro;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But if you put that piece with nested %IFs in a text file and include it in your code, it doesn’t work anymore:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;%macro TestMacro2;

&amp;nbsp;%include "…some path…\testinclude.txt";

%mend TestMacro2;

%TestMacro2;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Depending on the version you’re running you can get two different errors:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;ERROR: The %IF statement is not valid in open code.&lt;/LI&gt;&lt;LI&gt;ERROR: Nesting of %IF statements in open code is not supported.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I’ve looked for any documentation online but I can’t find anything.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Mar 2022 16:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/include-results-in-open-code/m-p/803594#M316430</guid>
      <dc:creator>SanderB</dc:creator>
      <dc:date>2022-03-23T16:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: %include results in open code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/include-results-in-open-code/m-p/803599#M316433</link>
      <description>&lt;P&gt;Your error message makes perfect sense.&amp;nbsp; You created a macro that generates ONE statement.&amp;nbsp; The %INCLUDE statement.&amp;nbsp; 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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the actual text in the included has definitely NOT been used in compiling the macro's definition.&amp;nbsp; Just the %INCLUDE statement itself becomes part of the compiled macro.&amp;nbsp; Try it with a %INCLUDE file that does work.&amp;nbsp; 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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Mar 2022 16:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/include-results-in-open-code/m-p/803599#M316433</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-23T16:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: %include results in open code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/include-results-in-open-code/m-p/803608#M316441</link>
      <description>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 &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;</description>
      <pubDate>Wed, 23 Mar 2022 17:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/include-results-in-open-code/m-p/803608#M316441</guid>
      <dc:creator>SanderB</dc:creator>
      <dc:date>2022-03-23T17:16:33Z</dc:date>
    </item>
  </channel>
</rss>

