<?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: Running macro using call execute in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820221#M323724</link>
    <description>&lt;P&gt;Thank you so much team for your kind help.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jun 2022 12:00:24 GMT</pubDate>
    <dc:creator>kajal_30</dc:creator>
    <dc:date>2022-06-24T12:00:24Z</dc:date>
    <item>
      <title>Running macro using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820044#M323650</link>
      <description>&lt;P&gt;I have a working macro %&amp;nbsp;&lt;/P&gt;&lt;P&gt;(yrmt = 202004);&lt;/P&gt;&lt;P&gt;I have 5 values for &amp;amp;yrmt&amp;nbsp; = 202007,202006,202004,202009.202010 ;&lt;/P&gt;&lt;P&gt;I want to run this macro for all these yrmt .can anyone please help.&lt;/P&gt;&lt;P&gt;Here is the code I have tried so far.&lt;/P&gt;&lt;PRE&gt;data dtmt;
	Do i= 1 to countw("&amp;amp;yrmt.");
		dttemp = scan("&amp;amp;yrmt.",i);
		call execute(%history_data(yrmt = dttemp));&lt;BR /&gt;output;&lt;BR /&gt;end;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Please advise&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Kajal&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 15:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820044#M323650</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2022-06-23T15:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Running macro using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820047#M323651</link>
      <description>&lt;P&gt;You have to build the string with the variable value and pass that to the macro. You're passing the text value of dttemp.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Creating the string in a separate value allows you to debug your code as well. You can verify that str is valid SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dtmt;
	Do i= 1 to countw("&amp;amp;yrmt.");
		dttemp = scan("&amp;amp;yrmt.",i);
             
                str =catt("%history_data(yrmt = ",
                              dttemp, 
                              ");"
                               );
                 call execute(str);
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jun 2022 15:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820047#M323651</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-23T15:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: Running macro using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820048#M323652</link>
      <description>&lt;P&gt;Let's first look at the code you wrote to see why it doesn't do what you want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's add an END for your DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dtmt;
  do i= 1 to countw("&amp;amp;yrmt.");
    dttemp = scan("&amp;amp;yrmt.",i);
    call execute(%history_data(yrmt = dttemp));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you are creating a variable named DTTEMP by extracting the next word in the macro variable YRMT.&amp;nbsp; That seems reasonable.&amp;nbsp;Note that DTTEMP will be a character variable but that should not matter.&amp;nbsp; Another thing to note is that you did not specify exactly what delimiter(s) to use in the COUNTW() or SCAN() functions.&amp;nbsp; So if the macro variables also have periods and spaces in addition to commas then you might not get the list of words you expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main issue that the CALL EXECUTE() function wants a STRING.&amp;nbsp; Instead you have just called the macro.&amp;nbsp; That means the macro processor will first run that macro call and all of the SAS code it generates will be dumped into the middle of your CALL EXECUTE() function.&amp;nbsp; You don't say what code the macro actually generates but let's assume that it generates a PROC SORT step.&amp;nbsp; So you tried to run something like this SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dtmt;
  do i= 1 to countw("202007,202006,202004,202009,202010");
    dttemp = scan("202007,202006,202004,202009,202010",i);
    call execute(proc sort ; by dttemp; run;);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Instead you want to generate a string to pass to CALL EXECUTE. So perhaps something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    call execute('%history_data(yrmt =' || dttemp || ')'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the single quotes to prevent the macro processor from trying to interpret %HISTORY_DATA as a macro call.&lt;/P&gt;
&lt;P&gt;But you should also get in the habit of preventing the macro processor from running the macro while CALL EXECUTE() is running.&amp;nbsp; You can delay its execution be wrapping its name in %NRSTR() macro function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    call execute('%nrstr(%history_data)(yrmt =' || dttemp || ')'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Try it both ways and notice the big difference in how the lines in the SAS log with + in the front that CALL EXECUTE() uses to show the code that ran.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are some other things you can do.&lt;/P&gt;
&lt;P&gt;1) Use the power of the DO statement.&amp;nbsp; You can use it to iterate of a series of values like:&amp;nbsp; DO X=1,3,4,6,9;&lt;/P&gt;
&lt;P&gt;2) Use the CATxx() series of functions to help build the code to push to CALL EXECUTE().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yrmt  = 202007,202006,202004,202009,202010 ;
data dtmt;
  do dttemp= &amp;amp;yrmt ;
    call execute(cats('%nrstr(%history_data)(yrmt =',dttemp,')'));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 15:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820048#M323652</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-23T15:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Running macro using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820050#M323653</link>
      <description>&lt;P&gt;As you didn't include a complete example i have made a test macro below. Here is one method by creating a macro program that calls your history_data macro call with all the values you listed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;options mprint mlogic symbolgen;&lt;BR /&gt;%macro history_data(yrt=);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%put its working...here is the value &amp;amp;yrmt;&lt;BR /&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro historycall(value);&lt;BR /&gt;%let i=1;&lt;BR /&gt;%do %until(&amp;amp;yrmt=);&lt;BR /&gt;%let yrmt=%scan(&amp;amp;value,&amp;amp;i);&lt;BR /&gt;%if &amp;amp;value= %then %put NOTE: Processing complete.;&lt;BR /&gt;%else %do; &lt;BR /&gt;%history_data(yrt = &amp;amp;yrmt); &lt;BR /&gt;%let i=%eval(&amp;amp;i+1);&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;%mend historycall;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%historycall(202007 202006 202004 202009 202010)&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2022 15:39:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820050#M323653</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2022-06-23T15:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: Running macro using call execute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820221#M323724</link>
      <description>&lt;P&gt;Thank you so much team for your kind help.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 12:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-macro-using-call-execute/m-p/820221#M323724</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2022-06-24T12:00:24Z</dc:date>
    </item>
  </channel>
</rss>

