<?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 Call Macro using %put generate error notice in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547350#M151669</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I write a macro and now the step to call it.&lt;/P&gt;
&lt;P&gt;I can list from line to line such as:&lt;/P&gt;
&lt;P&gt;%mymacro(age=1);&lt;/P&gt;
&lt;P&gt;%mymacro(age=2);&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;Instead I try 2 different method and the one with SQL and %put still works but generate all kind of error notice.&lt;/P&gt;
&lt;P&gt;Can you tell me why and how to fix it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have; set sashelp.class;run;

%Macro mymacro(age = );
data _temp; set have;
if age=&amp;amp;age;run;
			*if too small number of record --&amp;gt; no running code;
			proc sql;	select count(*) into: NOBS from _temp; quit;
	%IF &amp;amp;NOBS&amp;gt;=4 %THEN %DO;
		DATA A_&amp;amp;age; SET _temp; RUN;
	%END;
%Mend;

proc sort data=have  out=name_value (keep=age) nodupkey; by age;run;


*Method 1: work directly with file;
data _null_;
  set name_value;
  call execute(catt('%nrstr(%mymacro)(age=', age, ');'));
run;


*Method 2: create command first;
*create list of %mymacro(age=   ) for macro run;
data name_value;set name_value;
string =    catt('%mymacro(age=', age, ');');
run;

proc sql noprint;
select string into :codeline separated by ' ' from name_value; quit;

%put &amp;amp;codeline;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 29 Mar 2019 22:10:07 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2019-03-29T22:10:07Z</dc:date>
    <item>
      <title>Call Macro using %put generate error notice</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547350#M151669</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I write a macro and now the step to call it.&lt;/P&gt;
&lt;P&gt;I can list from line to line such as:&lt;/P&gt;
&lt;P&gt;%mymacro(age=1);&lt;/P&gt;
&lt;P&gt;%mymacro(age=2);&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;Instead I try 2 different method and the one with SQL and %put still works but generate all kind of error notice.&lt;/P&gt;
&lt;P&gt;Can you tell me why and how to fix it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have; set sashelp.class;run;

%Macro mymacro(age = );
data _temp; set have;
if age=&amp;amp;age;run;
			*if too small number of record --&amp;gt; no running code;
			proc sql;	select count(*) into: NOBS from _temp; quit;
	%IF &amp;amp;NOBS&amp;gt;=4 %THEN %DO;
		DATA A_&amp;amp;age; SET _temp; RUN;
	%END;
%Mend;

proc sort data=have  out=name_value (keep=age) nodupkey; by age;run;


*Method 1: work directly with file;
data _null_;
  set name_value;
  call execute(catt('%nrstr(%mymacro)(age=', age, ');'));
run;


*Method 2: create command first;
*create list of %mymacro(age=   ) for macro run;
data name_value;set name_value;
string =    catt('%mymacro(age=', age, ');');
run;

proc sql noprint;
select string into :codeline separated by ' ' from name_value; quit;

%put &amp;amp;codeline;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Mar 2019 22:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547350#M151669</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2019-03-29T22:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: Call Macro using %put generate error notice</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547354#M151672</link>
      <description>&lt;P&gt;Remove the %PUT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise the code is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%PUT %mymacro(age=11);;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which isn't valid code. If you just have:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;amp;codeLine;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It resolves to the following which is valid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(age=11);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;FYI - you may want to add the NOPRINT option to your SQL code to avoid output to the RESULTS pane.&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/49486"&gt;@hhchenfx&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I write a macro and now the step to call it.&lt;/P&gt;
&lt;P&gt;I can list from line to line such as:&lt;/P&gt;
&lt;P&gt;%mymacro(age=1);&lt;/P&gt;
&lt;P&gt;%mymacro(age=2);&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;Instead I try 2 different method and the one with SQL and %put still works but generate all kind of error notice.&lt;/P&gt;
&lt;P&gt;Can you tell me why and how to fix it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have; set sashelp.class;run;

%Macro mymacro(age = );
data _temp; set have;
if age=&amp;amp;age;run;
			*if too small number of record --&amp;gt; no running code;
			proc sql;	select count(*) into: NOBS from _temp; quit;
	%IF &amp;amp;NOBS&amp;gt;=4 %THEN %DO;
		DATA A_&amp;amp;age; SET _temp; RUN;
	%END;
%Mend;

proc sort data=have  out=name_value (keep=age) nodupkey; by age;run;


*Method 1: work directly with file;
data _null_;
  set name_value;
  call execute(catt('%nrstr(%mymacro)(age=', age, ');'));
run;


*Method 2: create command first;
*create list of %mymacro(age=   ) for macro run;
data name_value;set name_value;
string =    catt('%mymacro(age=', age, ');');
run;

proc sql noprint;
select string into :codeline separated by ' ' from name_value; quit;

%put &amp;amp;codeline;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2019 22:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547354#M151672</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-29T22:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: Call Macro using %put generate error notice</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547355#M151673</link>
      <description>&lt;P&gt;You are getting a timing issue with when the macro runs and when the code that the macro generates runs.&lt;/P&gt;
&lt;P&gt;When you call it normally as the macro runs the code it generates runs.&amp;nbsp; So the SQL code that sets NOBS macro variable runs and then the macro tests its values.&lt;/P&gt;
&lt;P&gt;But when you use CALL EXECUTE() to run it, without the %NRSTR(), then the macro runs while CALL EXECUTE is pushing code onto the stack to run after the data step finishes. So the PROC SQL code gets generated, but it won't execute until after the data step finishes. So the %IF statement that is checking NOBS macro variable is either not finding the variable to finding some value that was already set before the data step (the one running the CALL EXECUTE) started running.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check the difference in the lines with + that CALL EXECUTE shows in the log.&amp;nbsp; When you protect the macro call you will see lines like:&lt;/P&gt;
&lt;PRE&gt;+%mymacro(age=1);&lt;/PRE&gt;
&lt;P&gt;When you don't then instead you see lines like:&lt;/P&gt;
&lt;PRE&gt;+data _temp; 
+set have;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly if you put a %PUT in front of the macro call then it will "eat" the DATA _TEMP; statement.&amp;nbsp; So instead of running:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp; set have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are asking SAS to run:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put data _temp; 

set have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the SET statement has no DATA statement before it.&lt;/P&gt;
&lt;P&gt;The other calls included into the macro variable should work fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2019 22:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547355#M151673</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-29T22:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: Call Macro using %put generate error notice</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547364#M151676</link>
      <description>Look at DOSUBL instead of EXECUTE</description>
      <pubDate>Sat, 30 Mar 2019 01:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547364#M151676</guid>
      <dc:creator>CJac73</dc:creator>
      <dc:date>2019-03-30T01:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Call Macro using %put generate error notice</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547368#M151678</link>
      <description>&lt;P&gt;Very detail explanation!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot as usual, Tom.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Sat, 30 Mar 2019 01:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-Macro-using-put-generate-error-notice/m-p/547368#M151678</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2019-03-30T01:53:44Z</dc:date>
    </item>
  </channel>
</rss>

