<?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: Macro variable not getting resolved when use Call execute function in sas. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246988#M46269</link>
    <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much!!&lt;/P&gt;
&lt;P&gt;This works but do you mind explaining how using %NSTR solved that issue and also I couldn't understand why you used %% (double percent) before checkit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again!!&lt;/P&gt;
&lt;P&gt;Aprreciate your help.&lt;/P&gt;</description>
    <pubDate>Fri, 29 Jan 2016 21:28:48 GMT</pubDate>
    <dc:creator>anu1999</dc:creator>
    <dc:date>2016-01-29T21:28:48Z</dc:date>
    <item>
      <title>Macro variable not getting resolved when use Call execute function in sas.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246980#M46267</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Hi All,&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I am struggling with this issue where macro variables are not getting resolved&amp;nbsp;when i use call execute function in sas.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* Data set */&lt;/P&gt;
&lt;P&gt;data files ;&lt;BR /&gt; job = 1 ;&lt;BR /&gt; type = 'A' ;&lt;BR /&gt; group='ONE' ;&lt;BR /&gt; date='01jan2017'd ;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*macro */&lt;/P&gt;
&lt;P&gt;%macro checkit(xjob=, xtype=, xgroup=) ;&lt;/P&gt;
&lt;P&gt;%let xdate=;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt; select date into :xdate&lt;BR /&gt; from files&lt;BR /&gt; where job = &amp;amp;xjob.&lt;BR /&gt; and type = &amp;amp;xtype.&lt;BR /&gt; and group = &amp;amp;xgroup.;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;%put xdate = &amp;amp;xdate. ;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend checkit ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_ ;&lt;BR /&gt; set files ;&lt;BR /&gt; call execute("%checkit(xjob = 1, xtype = 'A', xgroup='ONE')") ;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;However If I execute macro like this (see below) it resolves xdate correctly.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%checkit(xjob = 1, xtype = 'A', xgroup='ONE') ;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Any idea why its happening and how can I fix it?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Thanks in advance.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 21:02:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246980#M46267</guid>
      <dc:creator>anu1999</dc:creator>
      <dc:date>2016-01-29T21:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not getting resolved when use Call execute function in sas.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246985#M46268</link>
      <description>&lt;P&gt;Don't use double quotes, otherwise the macro will be executed and the result put into the string that you are trying to push onto the command stack.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But even then you might have trouble since the macro will still execute when CALL EXECUTE tries to &amp;nbsp;push the macro call onto the stack. This means that the macro logic is executing BEFORE any of the code that it generates. &amp;nbsp;Hence the macro variable the code is going to generate doesn't exist yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to avoid this is to wrap the macro call inside of %NRSTR() macro function. &amp;nbsp;Then it will get pushed onto the stack as-is not execute until it is pulled from that stack after the data step ends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;
  set files ;
  call execute(cats(
     '%nrstr(%%checkit)'
    ,'(xjob =',job
    ,',xtype =',quote(type)
    ,',xgroup=',quote(group)
    ,')'
  ));
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 21:14:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246985#M46268</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-01-29T21:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not getting resolved when use Call execute function in sas.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246988#M46269</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much!!&lt;/P&gt;
&lt;P&gt;This works but do you mind explaining how using %NSTR solved that issue and also I couldn't understand why you used %% (double percent) before checkit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again!!&lt;/P&gt;
&lt;P&gt;Aprreciate your help.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 21:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246988#M46269</guid>
      <dc:creator>anu1999</dc:creator>
      <dc:date>2016-01-29T21:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not getting resolved when use Call execute function in sas.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246994#M46270</link>
      <description>&lt;P&gt;First look at the manual pages for the %STR() and %NRSTR() macro functions to understand why you need to double the %.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue is timing.&amp;nbsp;You can see it in action by looking at the lines in the SAS log with plus signs (+) in front of them showing the code that CALL EXECUTE generates. &amp;nbsp;When you use the %NRSTR() you will see that the line has the macro call. &amp;nbsp;Without that they will have the lines of code that the macro generates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try running this test and check the log carefully and see if you can figure out what happened.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro x(value);
 %let x=&amp;amp;value;
 data x; x=&amp;amp;value; run;
%mend x;
options mprint;
%let x=0;
%put Before DATA _NULL_  X=&amp;amp;x ;
data _null_;
  x=symget('x'); put 'BEFORE (1) ' x=;
  call execute("%x(1)");
  x=symget('x'); put 'BEFORE (2) ' x=;
  call execute('%x(2)');
  x=symget('x'); put 'BEFORE (3) ' x=;
  call execute('%nrstr(%%x)(3)');
  x=symget('x'); put 'AFTER  (3) ' x=;
run;
%put After DATA _NULL_  X=&amp;amp;x ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2016 21:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246994#M46270</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-01-29T21:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable not getting resolved when use Call execute function in sas.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246995#M46271</link>
      <description>&lt;P&gt;Yes, Just read it here:&amp;nbsp;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001061290.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001061290.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It makes sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 21:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/m-p/246995#M46271</guid>
      <dc:creator>anu1999</dc:creator>
      <dc:date>2016-01-29T21:49:09Z</dc:date>
    </item>
  </channel>
</rss>

