<?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 Problem calling a macro within a data step using call execute in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65183#M14148</link>
    <description>Hello, &lt;BR /&gt;
&lt;BR /&gt;
I’m trying to call a macro within a data step and only seems to be evaluating on the second run of the my program!&lt;BR /&gt;
&lt;BR /&gt;
For example, if i run the following code below, N, T1, T2 and T3 will be null even though the log shows the macro has run and set each global variable.&lt;BR /&gt;
&lt;BR /&gt;
Only when the program is run a second time (within the SAS session), dataset TEST is written too.&lt;BR /&gt;
&lt;BR /&gt;
I’m desperate to find a solution to this if possible? Why does it only compute on the second run of the SAS program.&lt;BR /&gt;
&lt;BR /&gt;
Thank you in advance. Lakes74.&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
Data look_up;&lt;BR /&gt;
&lt;BR /&gt;
input id D1 $ D2 $ D3 $;&lt;BR /&gt;
&lt;BR /&gt;
datalines;&lt;BR /&gt;
&lt;BR /&gt;
1 A B C&lt;BR /&gt;
&lt;BR /&gt;
2 D E F&lt;BR /&gt;
&lt;BR /&gt;
3 G H I&lt;BR /&gt;
&lt;BR /&gt;
4 J K L&lt;BR /&gt;
&lt;BR /&gt;
5 M N O&lt;BR /&gt;
&lt;BR /&gt;
6 P Q R&lt;BR /&gt;
&lt;BR /&gt;
7 S T U&lt;BR /&gt;
&lt;BR /&gt;
8 V W X&lt;BR /&gt;
&lt;BR /&gt;
9 Y Z A&lt;BR /&gt;
&lt;BR /&gt;
; &lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%global var1; &lt;BR /&gt;
&lt;BR /&gt;
%global var2; &lt;BR /&gt;
&lt;BR /&gt;
%global var3;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%macro temp(number);&lt;BR /&gt;
&lt;BR /&gt;
      proc sql noprint;&lt;BR /&gt;
&lt;BR /&gt;
            select D1,D2,D3 into :var1, :var2, :var3&lt;BR /&gt;
&lt;BR /&gt;
            from look_up&lt;BR /&gt;
&lt;BR /&gt;
            where id = &amp;amp;number;&lt;BR /&gt;
&lt;BR /&gt;
      quit;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%let id = 2;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
&lt;BR /&gt;
call execute ('%temp('||put(&amp;amp;id,3.)||')');&lt;BR /&gt;
&lt;BR /&gt;
N = "&amp;amp;id";&lt;BR /&gt;
&lt;BR /&gt;
T1 = "&amp;amp;var1";&lt;BR /&gt;
&lt;BR /&gt;
T2 = "&amp;amp;var2";&lt;BR /&gt;
&lt;BR /&gt;
T3 = "&amp;amp;var3";&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;var1;&lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;var2;&lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;var3;</description>
    <pubDate>Fri, 05 Mar 2010 13:51:30 GMT</pubDate>
    <dc:creator>Lakes74</dc:creator>
    <dc:date>2010-03-05T13:51:30Z</dc:date>
    <item>
      <title>Problem calling a macro within a data step using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65183#M14148</link>
      <description>Hello, &lt;BR /&gt;
&lt;BR /&gt;
I’m trying to call a macro within a data step and only seems to be evaluating on the second run of the my program!&lt;BR /&gt;
&lt;BR /&gt;
For example, if i run the following code below, N, T1, T2 and T3 will be null even though the log shows the macro has run and set each global variable.&lt;BR /&gt;
&lt;BR /&gt;
Only when the program is run a second time (within the SAS session), dataset TEST is written too.&lt;BR /&gt;
&lt;BR /&gt;
I’m desperate to find a solution to this if possible? Why does it only compute on the second run of the SAS program.&lt;BR /&gt;
&lt;BR /&gt;
Thank you in advance. Lakes74.&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
Data look_up;&lt;BR /&gt;
&lt;BR /&gt;
input id D1 $ D2 $ D3 $;&lt;BR /&gt;
&lt;BR /&gt;
datalines;&lt;BR /&gt;
&lt;BR /&gt;
1 A B C&lt;BR /&gt;
&lt;BR /&gt;
2 D E F&lt;BR /&gt;
&lt;BR /&gt;
3 G H I&lt;BR /&gt;
&lt;BR /&gt;
4 J K L&lt;BR /&gt;
&lt;BR /&gt;
5 M N O&lt;BR /&gt;
&lt;BR /&gt;
6 P Q R&lt;BR /&gt;
&lt;BR /&gt;
7 S T U&lt;BR /&gt;
&lt;BR /&gt;
8 V W X&lt;BR /&gt;
&lt;BR /&gt;
9 Y Z A&lt;BR /&gt;
&lt;BR /&gt;
; &lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%global var1; &lt;BR /&gt;
&lt;BR /&gt;
%global var2; &lt;BR /&gt;
&lt;BR /&gt;
%global var3;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%macro temp(number);&lt;BR /&gt;
&lt;BR /&gt;
      proc sql noprint;&lt;BR /&gt;
&lt;BR /&gt;
            select D1,D2,D3 into :var1, :var2, :var3&lt;BR /&gt;
&lt;BR /&gt;
            from look_up&lt;BR /&gt;
&lt;BR /&gt;
            where id = &amp;amp;number;&lt;BR /&gt;
&lt;BR /&gt;
      quit;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%let id = 2;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
&lt;BR /&gt;
call execute ('%temp('||put(&amp;amp;id,3.)||')');&lt;BR /&gt;
&lt;BR /&gt;
N = "&amp;amp;id";&lt;BR /&gt;
&lt;BR /&gt;
T1 = "&amp;amp;var1";&lt;BR /&gt;
&lt;BR /&gt;
T2 = "&amp;amp;var2";&lt;BR /&gt;
&lt;BR /&gt;
T3 = "&amp;amp;var3";&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;var1;&lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;var2;&lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;var3;</description>
      <pubDate>Fri, 05 Mar 2010 13:51:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65183#M14148</guid>
      <dc:creator>Lakes74</dc:creator>
      <dc:date>2010-03-05T13:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: Problem calling a macro within a data step using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65184#M14149</link>
      <description>You are attempting to mix SAS MACRO language, macro variable usage, and SAS DATA step processing, which normally is not going to work properly.  Read up on CALL EXECUTE and you'll see that the invoked code will actually execute *AFTER* the current DATA step completes.&lt;BR /&gt;
&lt;BR /&gt;
Suggest adding the code below to get the most output in your SAS log with your compiled and executed code:&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MPRINT MLOGIC;&lt;BR /&gt;
&lt;BR /&gt;
If you have a particular need to use SAS macro language facilities, it's best to get your code working without macros, understand the flow such that it works properly, and then decide what's needed with macros, if at all, to enhance or make your program more effective/efficient by using SAS macros and macro variables.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Recommended Google advanced search arguments, this topic/post:&lt;BR /&gt;
&lt;BR /&gt;
macro language introduction site:sas.com&lt;BR /&gt;
&lt;BR /&gt;
macro variable scoping site:sas.com</description>
      <pubDate>Fri, 05 Mar 2010 13:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65184#M14149</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-03-05T13:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: Problem calling a macro within a data step using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65185#M14150</link>
      <description>Agree with Scott.&lt;BR /&gt;
&lt;BR /&gt;
Mixing macro variables with dataset variables could be quite challenging.&lt;BR /&gt;
&lt;BR /&gt;
From your example you are trying to read a dataset from within a dataset.&lt;BR /&gt;
&lt;BR /&gt;
This could be done with some other techniques, like index match, hashing or even using the SAS File I/O functions.&lt;BR /&gt;
&lt;BR /&gt;
Check the online documentation and examples:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000148395.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000148395.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Fri, 05 Mar 2010 18:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65185#M14150</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-03-05T18:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Problem calling a macro within a data step using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65186#M14151</link>
      <description>You can't run proc sql inside a data step, think about it.&lt;BR /&gt;
&lt;BR /&gt;
2 ways to do what you want: &lt;BR /&gt;
&lt;BR /&gt;
Either look up the data some other way (no need to call a macro to look up data), or if you need to do more complex things in your macro, it has to be pure macro language for the data step to run it immediately.&lt;BR /&gt;
&lt;BR /&gt;
This does both:&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data look_up(index=(ID));&lt;BR /&gt;
  input id D1 $ D2 $ D3 $;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 A B C&lt;BR /&gt;
2 D E F&lt;BR /&gt;
3 G H I&lt;BR /&gt;
4 J K L&lt;BR /&gt;
5 M N O&lt;BR /&gt;
6 P Q R&lt;BR /&gt;
7 S T U&lt;BR /&gt;
8 V W X&lt;BR /&gt;
9 Y Z A&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro temp(number);&lt;BR /&gt;
  %global var1 var2 var3;&lt;BR /&gt;
  %let dsid=%sysfunc(open(look_up(where=(ID = &amp;amp;number))));&lt;BR /&gt;
  %let rc=%sysfunc(fetch(&amp;amp;dsid));&lt;BR /&gt;
  %let var1=%sysfunc(getvarc(&amp;amp;dsid,%sysfunc(varnum(&amp;amp;dsid,D1))));&lt;BR /&gt;
  %let var2=%sysfunc(getvarc(&amp;amp;dsid,%sysfunc(varnum(&amp;amp;dsid,D2))));&lt;BR /&gt;
  %let var3=%sysfunc(getvarc(&amp;amp;dsid,%sysfunc(varnum(&amp;amp;dsid,D3))));&lt;BR /&gt;
  %let rc=%sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%let id = 2;&lt;BR /&gt;
&lt;BR /&gt;
data TEST;&lt;BR /&gt;
  call execute ('%temp('||put(&amp;amp;id,3.)||')');  *get values from macro;&lt;BR /&gt;
  T1 = symget('var1');&lt;BR /&gt;
  T2 = symget('var2');&lt;BR /&gt;
  T3 = symget('var3');&lt;BR /&gt;
  output;&lt;BR /&gt;
  ID = &amp;amp;id+1;&lt;BR /&gt;
  set look_up key=ID;                         *get values from look-up key&lt;BR /&gt;
  T1 = D1;&lt;BR /&gt;
  T2 = D2;&lt;BR /&gt;
  T3 = D3;&lt;BR /&gt;
  output;&lt;BR /&gt;
  stop;&lt;BR /&gt;
run;[/pre]</description>
      <pubDate>Mon, 08 Mar 2010 22:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65186#M14151</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2010-03-08T22:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: Problem calling a macro within a data step using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65187#M14152</link>
      <description>Thanks all for the helpful replies, really appreciate it!</description>
      <pubDate>Tue, 18 May 2010 10:43:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-calling-a-macro-within-a-data-step-using-call-execute/m-p/65187#M14152</guid>
      <dc:creator>Lakes74</dc:creator>
      <dc:date>2010-05-18T10:43:25Z</dc:date>
    </item>
  </channel>
</rss>

