<?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 execution timing of macro generated code vs macro invocation. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/execution-timing-of-macro-generated-code-vs-macro-invocation/m-p/879471#M347432</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Came across something today that challenged my understanding of how I thought macro code worked vs the code the macro generates.&lt;/P&gt;
&lt;P&gt;I've always though macro invocation completed prior to any of the code it generated getting run.&amp;nbsp; Apparently that isn't the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the log from a simple test using sashelp.baseball.&amp;nbsp; Note that the 1st observation in the baseball dataset does have Cleveland as a value, so the team macro variable would be created at the end of the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was under the impression though that the entire macro runs prior to any code it generates is run.&amp;nbsp; If that were the case, the team macro variable below would not exist prior to the %if checks after the macro was run.&amp;nbsp;&amp;nbsp;&amp;nbsp; What I'm seeing is the macro processor is feeding statements to the input stream of the SAS processor and running the code at each step boundary.&amp;nbsp;&amp;nbsp;&amp;nbsp; Am I reading this correctly?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Ben&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4715&amp;nbsp; %macro checkteam;&lt;/P&gt;
&lt;P&gt;4716&amp;nbsp; data _null_;&lt;/P&gt;
&lt;P&gt;4717&amp;nbsp; set sashelp.baseball;&lt;/P&gt;
&lt;P&gt;4718&amp;nbsp; if team='Cleveland' then do;&lt;/P&gt;
&lt;P&gt;4719&amp;nbsp;&amp;nbsp;&amp;nbsp; call symputx('team',team);&lt;/P&gt;
&lt;P&gt;4720&amp;nbsp;&amp;nbsp;&amp;nbsp; stop;&lt;/P&gt;
&lt;P&gt;4721&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;4722&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;4723&lt;/P&gt;
&lt;P&gt;4724&amp;nbsp; %put &amp;amp;=team;&lt;/P&gt;
&lt;P&gt;4725&amp;nbsp; %if &amp;amp;team=Oakland %then %put It was Oakdland;&lt;/P&gt;
&lt;P&gt;4726&amp;nbsp; %if &amp;amp;team=Cleveland %then %put It was Cleveland;&lt;/P&gt;
&lt;P&gt;4727&amp;nbsp; %mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4728&amp;nbsp; options mlogic symbolgen mprint;&lt;/P&gt;
&lt;P&gt;4729&lt;/P&gt;
&lt;P&gt;4730&amp;nbsp; %put &amp;amp;=team;&lt;/P&gt;
&lt;P&gt;WARNING: Apparent symbolic reference TEAM not resolved.&lt;/P&gt;
&lt;P&gt;team&lt;/P&gt;
&lt;P&gt;4731&amp;nbsp; %checkteam;&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; Beginning execution.&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; data _null_;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; set sashelp.baseball;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; if team='Cleveland' then do;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; call symputx('team',team);&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; stop;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: There were 1 observations read from the data set SASHELP.BASEBALL.&lt;/P&gt;
&lt;P&gt;NOTE: DATA statement used (Total process time):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.00 seconds&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %PUT &amp;amp;=team&lt;/P&gt;
&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable TEAM resolves to Cleveland&lt;/P&gt;
&lt;P&gt;TEAM=Cleveland&lt;/P&gt;
&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable TEAM resolves to Cleveland&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %IF condition &amp;amp;team=Oakland is FALSE&lt;/P&gt;
&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable TEAM resolves to Cleveland&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %IF condition &amp;amp;team=Cleveland is TRUE&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %PUT It was Cleveland&lt;/P&gt;
&lt;P&gt;It was Cleveland&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; Ending execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 07 Jun 2023 03:03:24 GMT</pubDate>
    <dc:creator>BenConner</dc:creator>
    <dc:date>2023-06-07T03:03:24Z</dc:date>
    <item>
      <title>execution timing of macro generated code vs macro invocation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execution-timing-of-macro-generated-code-vs-macro-invocation/m-p/879471#M347432</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Came across something today that challenged my understanding of how I thought macro code worked vs the code the macro generates.&lt;/P&gt;
&lt;P&gt;I've always though macro invocation completed prior to any of the code it generated getting run.&amp;nbsp; Apparently that isn't the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the log from a simple test using sashelp.baseball.&amp;nbsp; Note that the 1st observation in the baseball dataset does have Cleveland as a value, so the team macro variable would be created at the end of the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was under the impression though that the entire macro runs prior to any code it generates is run.&amp;nbsp; If that were the case, the team macro variable below would not exist prior to the %if checks after the macro was run.&amp;nbsp;&amp;nbsp;&amp;nbsp; What I'm seeing is the macro processor is feeding statements to the input stream of the SAS processor and running the code at each step boundary.&amp;nbsp;&amp;nbsp;&amp;nbsp; Am I reading this correctly?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Ben&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4715&amp;nbsp; %macro checkteam;&lt;/P&gt;
&lt;P&gt;4716&amp;nbsp; data _null_;&lt;/P&gt;
&lt;P&gt;4717&amp;nbsp; set sashelp.baseball;&lt;/P&gt;
&lt;P&gt;4718&amp;nbsp; if team='Cleveland' then do;&lt;/P&gt;
&lt;P&gt;4719&amp;nbsp;&amp;nbsp;&amp;nbsp; call symputx('team',team);&lt;/P&gt;
&lt;P&gt;4720&amp;nbsp;&amp;nbsp;&amp;nbsp; stop;&lt;/P&gt;
&lt;P&gt;4721&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;4722&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;4723&lt;/P&gt;
&lt;P&gt;4724&amp;nbsp; %put &amp;amp;=team;&lt;/P&gt;
&lt;P&gt;4725&amp;nbsp; %if &amp;amp;team=Oakland %then %put It was Oakdland;&lt;/P&gt;
&lt;P&gt;4726&amp;nbsp; %if &amp;amp;team=Cleveland %then %put It was Cleveland;&lt;/P&gt;
&lt;P&gt;4727&amp;nbsp; %mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4728&amp;nbsp; options mlogic symbolgen mprint;&lt;/P&gt;
&lt;P&gt;4729&lt;/P&gt;
&lt;P&gt;4730&amp;nbsp; %put &amp;amp;=team;&lt;/P&gt;
&lt;P&gt;WARNING: Apparent symbolic reference TEAM not resolved.&lt;/P&gt;
&lt;P&gt;team&lt;/P&gt;
&lt;P&gt;4731&amp;nbsp; %checkteam;&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; Beginning execution.&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; data _null_;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; set sashelp.baseball;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; if team='Cleveland' then do;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; call symputx('team',team);&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; stop;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;MPRINT(CHECKTEAM):&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: There were 1 observations read from the data set SASHELP.BASEBALL.&lt;/P&gt;
&lt;P&gt;NOTE: DATA statement used (Total process time):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.00 seconds&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %PUT &amp;amp;=team&lt;/P&gt;
&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable TEAM resolves to Cleveland&lt;/P&gt;
&lt;P&gt;TEAM=Cleveland&lt;/P&gt;
&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable TEAM resolves to Cleveland&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %IF condition &amp;amp;team=Oakland is FALSE&lt;/P&gt;
&lt;P&gt;SYMBOLGEN:&amp;nbsp; Macro variable TEAM resolves to Cleveland&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %IF condition &amp;amp;team=Cleveland is TRUE&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; %PUT It was Cleveland&lt;/P&gt;
&lt;P&gt;It was Cleveland&lt;/P&gt;
&lt;P&gt;MLOGIC(CHECKTEAM):&amp;nbsp; Ending execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 03:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execution-timing-of-macro-generated-code-vs-macro-invocation/m-p/879471#M347432</guid>
      <dc:creator>BenConner</dc:creator>
      <dc:date>2023-06-07T03:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: execution timing of macro generated code vs macro invocation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execution-timing-of-macro-generated-code-vs-macro-invocation/m-p/879472#M347433</link>
      <description>&lt;P&gt;As the macro emits SAS code the code is compiled and run the same way it would have run if that same code was hard coded into the source file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So when the macro finishes emitting a DATA step the data step runs.&amp;nbsp; When that step is finished the macro processor can continue emitting new code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where you might experience what you thought would happen is if you submit a macro call with CALL EXECUTE().&amp;nbsp; If you do not protect the macro call with %NRSTR() then the macro execute while CALL EXECUTE is storing the code onto the stack to run after the current data step runs.&amp;nbsp; So the data step does not actually have a chance to run (it is waiting for the step that submitted it to finish) so the macro logic executes before the data step runs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To see this with your macro run these two data steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call execute('%checkteam;');
run;
data _null_;
  call execute('%nrstr(%checkteam);');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 03:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execution-timing-of-macro-generated-code-vs-macro-invocation/m-p/879472#M347433</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-07T03:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: execution timing of macro generated code vs macro invocation.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/execution-timing-of-macro-generated-code-vs-macro-invocation/m-p/879473#M347434</link>
      <description>Thanks, Tom.  Good thing I never relied on that for code to work. Wow. &lt;BR /&gt;&lt;BR /&gt;Best wishes,&lt;BR /&gt;Ben&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 07 Jun 2023 03:22:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/execution-timing-of-macro-generated-code-vs-macro-invocation/m-p/879473#M347434</guid>
      <dc:creator>BenConner</dc:creator>
      <dc:date>2023-06-07T03:22:11Z</dc:date>
    </item>
  </channel>
</rss>

