<?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 Re: Variable processing with macros in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/498128#M132281</link>
    <description>&lt;P&gt;Some languages execute each statement in order, as the statement is submitted.&amp;nbsp; SAS does not.&amp;nbsp; Consider the end of your program:&lt;/P&gt;
&lt;PRE&gt;data work.temp2;
set WORK.temp;
putlog VARNUM; 
call execute('%getVarNumber(vn=20);');
temp=&amp;amp;varName2;
putlog temp; 
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS sees the DATA statement and figures out what it means.&amp;nbsp; But it doesn't execute anything yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sees the SET statement, and same thing ... figure out what it means but don't execute anything yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Same for all the statements in the DATA step ... SAS figures out what they mean, but doesn't execute anything yet.&amp;nbsp; As part of that process, this statement must use the previously assigned value for &amp;amp;varName2:&amp;nbsp; temp=&amp;amp;varName2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When SAS finally encounters the RUN statement, it knows that it has all the statements related to the DATA step and actually runs them.&amp;nbsp; But the value being assigned to TEMP depends on the value of &amp;amp;varName2 before the DATA step began to execute.&amp;nbsp; That value was in effect when SAS "figured out" what temp=&amp;amp;varName2; actually meant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 22 Sep 2018 21:51:01 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-09-22T21:51:01Z</dc:date>
    <item>
      <title>Variable processing with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/497978#M132191</link>
      <description>&lt;P&gt;I have encountered the following behavior in SAS EG (Version 7.11 64-bit) that I am struggling to understand.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run the code below, I see the value of &lt;EM&gt;tempvar&lt;/EM&gt; in &lt;EM&gt;work.temp2&lt;/EM&gt; get set to '20'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I edit the code line&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;call execute('%getVarNumber(vn=20);');&lt;/PRE&gt;&lt;P&gt;to&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;call execute('%getVarNumber(vn=30);');&lt;/PRE&gt;&lt;P&gt;but when I run the code the value of&amp;nbsp;&lt;SPAN&gt;&lt;EM&gt;tempvar&lt;/EM&gt; in &lt;EM&gt;work.temp2 &lt;/EM&gt;remains '20' on the first run but updates to '30' the second time I run it.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CODE:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA work.temp;
INPUT A;
DATALINES;
1 
4 
;



%macro getVarNumber (vn=);
%global varName2;
%let varName2=&amp;amp;vn;
%put &amp;amp;varName2;
%mend getVarNumber;



data work.temp2;
set WORK.temp;
putlog VARNUM; 
call execute('%getVarNumber(vn=20);');
temp=&amp;amp;varName2;
putlog temp; 
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 20:23:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/497978#M132191</guid>
      <dc:creator>mk123451243</dc:creator>
      <dc:date>2018-09-21T20:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Variable processing with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/497988#M132194</link>
      <description>&lt;P&gt;What are you actually attempting to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CALL EXECUTE creates code that actually executes after the data step that makes the calls ends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And when I run this code I get this error (not unexpected actually the first time)&lt;/P&gt;
&lt;PRE&gt;781  data work.temp2;
782  set WORK.temp;
783  putlog VARNUM;
784  call execute('%getVarNumber(vn=20);');
785  temp=&amp;amp;varName2;
          -
          22
WARNING: Apparent symbolic reference VARNAME2 not resolved.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,
              a numeric constant, a datetime constant, a missing value, INPUT, PUT.

786  putlog temp;
787  run;
&lt;/PRE&gt;
&lt;P&gt;Macro variables in a data step such as your &amp;amp;varname2 are resolved as the data step compiles, way before any actual data&amp;nbsp; is read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you are getting the macro value from the LAST time you ran that set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also in general because of that timing you can't easily reference the value of a macro variable in the data step it is created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 21:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/497988#M132194</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-21T21:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: Variable processing with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/498007#M132202</link>
      <description>&lt;P&gt;CALL EXECUTE works after the data step is done.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to resolve a macro variable within the data step, use SYMGET function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/92121"&gt;@mk123451243&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have encountered the following behavior in SAS EG (Version 7.11 64-bit) that I am struggling to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run the code below, I see the value of &lt;EM&gt;tempvar&lt;/EM&gt; in &lt;EM&gt;work.temp2&lt;/EM&gt; get set to '20'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I edit the code line&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;call execute('%getVarNumber(vn=20);');&lt;/PRE&gt;
&lt;P&gt;to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;call execute('%getVarNumber(vn=30);');&lt;/PRE&gt;
&lt;P&gt;but when I run the code the value of&amp;nbsp;&lt;SPAN&gt;&lt;EM&gt;tempvar&lt;/EM&gt; in &lt;EM&gt;work.temp2 &lt;/EM&gt;remains '20' on the first run but updates to '30' the second time I run it.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CODE:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA work.temp;
INPUT A;
DATALINES;
1 
4 
;



%macro getVarNumber (vn=);
%global varName2;
%let varName2=&amp;amp;vn;
%put &amp;amp;varName2;
%mend getVarNumber;



data work.temp2;
set WORK.temp;
putlog VARNUM; 
call execute('%getVarNumber(vn=20);');
temp=&amp;amp;varName2;
putlog temp; 
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Sep 2018 00:11:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/498007#M132202</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-22T00:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: Variable processing with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/498064#M132245</link>
      <description>&lt;P&gt;The macro processor processes the code and resolves macro variable references and then passes the result to SAS to execute.&amp;nbsp; CALL EXECUTE() pushes code onto a stack to execute after the current data step finishes.&amp;nbsp; Your first attempt should have given a warning that VARNAME2 was not defined.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically you ran this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varname2=30;
data work.temp2;
  set WORK.temp;
  putlog VARNUM; 
  call execute('%getVarNumber(vn=20);');
  temp=30;
  putlog temp; 
run;
%let varname2=20;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 22 Sep 2018 12:03:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/498064#M132245</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-22T12:03:22Z</dc:date>
    </item>
    <item>
      <title>Re: Variable processing with macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/498128#M132281</link>
      <description>&lt;P&gt;Some languages execute each statement in order, as the statement is submitted.&amp;nbsp; SAS does not.&amp;nbsp; Consider the end of your program:&lt;/P&gt;
&lt;PRE&gt;data work.temp2;
set WORK.temp;
putlog VARNUM; 
call execute('%getVarNumber(vn=20);');
temp=&amp;amp;varName2;
putlog temp; 
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS sees the DATA statement and figures out what it means.&amp;nbsp; But it doesn't execute anything yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It sees the SET statement, and same thing ... figure out what it means but don't execute anything yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Same for all the statements in the DATA step ... SAS figures out what they mean, but doesn't execute anything yet.&amp;nbsp; As part of that process, this statement must use the previously assigned value for &amp;amp;varName2:&amp;nbsp; temp=&amp;amp;varName2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When SAS finally encounters the RUN statement, it knows that it has all the statements related to the DATA step and actually runs them.&amp;nbsp; But the value being assigned to TEMP depends on the value of &amp;amp;varName2 before the DATA step began to execute.&amp;nbsp; That value was in effect when SAS "figured out" what temp=&amp;amp;varName2; actually meant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Sep 2018 21:51:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-processing-with-macros/m-p/498128#M132281</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-09-22T21:51:01Z</dc:date>
    </item>
  </channel>
</rss>

