<?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: Repeat a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102047#M21354</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Chang, thanks for the reply but...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Non nesting the macro would imply renaming all invocations of %macr to %doOver (your suggestion), while nesting it will allow you to keep the original name. That's really why I've nested the macro and renamed the parent to the original name.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As for the recursiveness, I believe what I've showed can't be considered recursive, as there is no recursive feedback from the macro to the previous iteration. But I get your point, self-calling the macro just obfuscates the coding, which I really don't like it, also it reminded me what was my mindset at the beginning which I guess I lost on the way. Thank you for pointing that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So here's the "revisited" code, shorter, polished and truly recursive which allows any form of mixed var list and ranges: (&amp;lt;X1 X2 ... XN&amp;gt;|&amp;lt;Y1-YN&amp;gt;)*&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; macr(VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* expands range recursively;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; expand(VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%if %index(%str(&amp;amp;VARLIST),%str(-)) %then %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%let PRX=%sysfunc(prxparse(s!(([_A-Z]+\d+\s+)*)([_A-Z]+)(\d+)[-]{1}([_A-Z]+)(\d+)!$3 $4 $6|$1!i));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%let PRX=%sysfunc(prxchange(&amp;amp;PRX,1,&amp;amp;VARLIST));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; VARLIST;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%do _I=%scan(&amp;amp;PRX,2) %to %scan(&amp;amp;PRX,3);%scan(&amp;amp;PRX,1)&amp;amp;_I%str( )%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; VARLIST;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%sysfunc(compbl(%VARLIST%expand(%scan(&amp;amp;PRX,2,%str(|)))))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%else %str(&amp;amp;VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; expand;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* single macro var code here;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; _macr(VAR);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%put _macr(&amp;amp;VAR);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; _macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* single macro var code here;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%let VARLIST=%expand(&amp;amp;VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%do _I=1 %to %sysfunc(countc(&amp;amp;VARLIST,%str( )))+1; * iterate through var list;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%_macr(%scan(%str(&amp;amp;VARLIST),&amp;amp;_I,%str( )));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* so now, everything is possible;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(&lt;STRONG&gt;X1-X10&lt;/STRONG&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(&lt;STRONG&gt;X1-X10&lt;/STRONG&gt; &lt;STRONG&gt;Y1-Y5&lt;/STRONG&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(&lt;STRONG&gt;X1-X10&lt;/STRONG&gt; Y1 Y3 Y6 Y8 Y9);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(X1 X3 X6 X8 X10 &lt;STRONG&gt;Y1-Y5&lt;/STRONG&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(X1 X4 &lt;STRONG&gt;Y1-Y5&lt;/STRONG&gt; X5 X10);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(X1 X3 X4 X7 X10);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Thanks for the reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers from Portugal.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 25 Oct 2012 09:55:06 GMT</pubDate>
    <dc:creator>DanielSantos</dc:creator>
    <dc:date>2012-10-25T09:55:06Z</dc:date>
    <item>
      <title>Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102038#M21345</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I've created a macro that I would like to run with 10 differents variables. I was wondering if there is a better and simple way than calling this macro 10 times, i.e.:&amp;nbsp; &lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;macr&lt;/EM&gt;&lt;/STRONG&gt;(var1); %&lt;STRONG&gt;&lt;EM&gt;macr&lt;/EM&gt;&lt;/STRONG&gt;(var2);...%&lt;STRONG&gt;&lt;EM&gt;macr&lt;/EM&gt;&lt;/STRONG&gt;(var10);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;macr&lt;/EM&gt;&lt;/STRONG&gt;(var1-var10) doesn't work and I notice that array/do cannot be used for this purpose neither.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Oct 2012 19:17:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102038#M21345</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2012-10-19T19:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102039#M21346</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are a number of alternatives.&amp;nbsp; Take a look at: &lt;A href="http://www.lexjansen.com/pnwsug/2004/c_cc_storing_and_using_a_lis.pdf" title="http://www.lexjansen.com/pnwsug/2004/c_cc_storing_and_using_a_lis.pdf"&gt;http://www.lexjansen.com/pnwsug/2004/c_cc_storing_and_using_a_lis.pdf&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Oct 2012 19:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102039#M21346</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2012-10-19T19:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102040#M21347</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;One issue to watch for when you reuse macros is to eliminate redundant processing of the data.&amp;nbsp; For example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro extra_work (varname);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; proc sort data=mydata out=temp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by year;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; proc univariate data=temp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by year;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; var &amp;amp;varname;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now each time you use the macro, it sorts the data.&amp;nbsp; But sorting the data just once would have been sufficient.&amp;nbsp; This pitfall takes many forms, but it is something you should watch for.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Oct 2012 20:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102040#M21347</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2012-10-19T20:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102041#M21348</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Really depends on what the macro is doing, can you modify the macro, etc.&lt;/P&gt;&lt;P&gt;My preference is to pass in variable lists as space delimited lists of variable names, just as you would in a normal SAS statements such as BY, VAR, KEEP, DROP etc.&lt;/P&gt;&lt;P&gt;You might be able to just put that list into the proper place in the code that the macro is generating.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro macr(varlist);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; proc means ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var &amp;amp;varlist ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%mend macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or you might need to loop over the list inside the macro&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro macr(varlist);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; %local i ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; %do i=1 %to %sysfunc(countw(&amp;amp;varlist,%str( )));&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; proc means ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var %scan(&amp;amp;varlist,&amp;amp;i,%str( )) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; %end ;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%mend macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Oct 2012 20:25:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102041#M21348</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-10-19T20:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102042#M21349</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You may also try the PARMBUFF option with subsequent SYSPBUFF parsing.&lt;/P&gt;&lt;P&gt;Please see&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.lexjansen.com/pharmasug/2006/technicaltechniques/tt28.pdf"&gt;http://www.lexjansen.com/pharmasug/2006/technicaltechniques/tt28.pdf&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Oct 2012 21:02:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102042#M21349</guid>
      <dc:creator>joehinson</dc:creator>
      <dc:date>2012-10-19T21:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102043#M21350</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Call execute in a Data _Null_ with a do loop may be what you are looking for&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%Let varlist = var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data _Null_ ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Retain varlist "&amp;amp;varlist" ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Do k = 1 to 10 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; usevar = scan (varlist, k) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Call execute ('%Let usevar = ' || usevar || ';') ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Call execute ('%macr (&amp;amp;usevar) ;') ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End ;&lt;/P&gt;&lt;P&gt;Run ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;(Not tested!)&lt;/P&gt;&lt;P&gt;Note use of single quoted&lt;/P&gt;&lt;P&gt;Note use of %Let statements rather than call symput, to ensure that macro values are assigned in sequence.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Oct 2012 23:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102043#M21350</guid>
      <dc:creator>RichardinOz</dc:creator>
      <dc:date>2012-10-19T23:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102044#M21351</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks RichardinOz, it works perfectly. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 21 Oct 2012 19:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102044#M21351</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2012-10-21T19:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102045#M21352</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you don't mind the "twist", here's another option, macro based only, that encapsulates your macro into a second one which will handle the list of variables. Something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: navy; font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt; macr(VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%if %index(%str(&amp;amp;VARLIST),%str(-)) %then %do; * is it a range?;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%let LDX=%sysfunc(anydigit(%str(&amp;amp;VARLIST))); &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%let PFX=%substr(%str(&amp;amp;VARLIST),1,&amp;amp;LDX-1); * get prefix;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%let LDX=%substr(%scan(%str(&amp;amp;VARLIST),1,%str(-)),&amp;amp;LDX); &lt;SPAN style="color: #000080; font-family: 'Courier New';"&gt;* get lower index;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%let HDX=%sysfunc(anydigit(%scan(%str(&amp;amp;VARLIST),2,%str(-))));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%let HDX=%substr(%scan(%str(&amp;amp;VARLIST),2,%str(-)),&amp;amp;HDX); &lt;SPAN style="color: #000080; font-family: 'Courier New';"&gt;* get higher index;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: navy; font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt; &lt;STRONG&gt;&lt;EM&gt;VARLIST&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%do _I=&amp;amp;LDX %to &amp;amp;HDX-&lt;STRONG&gt;1&lt;/STRONG&gt;;&amp;amp;PFX&amp;amp;_I%str( )%end;&amp;amp;PFX&amp;amp;HDX&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: navy; font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt; VARLIST;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%&lt;STRONG&gt;&lt;EM&gt;macr&lt;/EM&gt;&lt;/STRONG&gt;(%&lt;STRONG&gt;&lt;EM&gt;VARLIST&lt;/EM&gt;&lt;/STRONG&gt;); * resubmit the list, no range this time;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%else %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;* single macro var code here;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: navy; font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt; _macr(VAR);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%put _macr(&amp;amp;VAR);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: navy; font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt; _macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;* single macro var code here;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%do _I=&lt;STRONG&gt;1&lt;/STRONG&gt; %to %sysfunc(countc(%str(&amp;amp;VARLIST),%str( )))+1; &lt;SPAN style="color: #000080; font-family: 'Courier New';"&gt;* iterate through var list;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%&lt;STRONG&gt;&lt;EM&gt;_macr&lt;/EM&gt;&lt;/STRONG&gt;(%scan(%str(&amp;amp;VARLIST),&amp;amp;_I,%str( ))); &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: navy; font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt; macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000080; font-family: 'Courier New';"&gt;* testing it;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%&lt;STRONG&gt;&lt;EM&gt;macr&lt;/EM&gt;&lt;/STRONG&gt;(X1-X10);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New'; color: navy;"&gt;%&lt;STRONG&gt;&lt;EM&gt;macr&lt;/EM&gt;&lt;/STRONG&gt;(X1 X3 X4 X5 X10);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Allows a range or list of separated variable, not both.&lt;/P&gt;&lt;P&gt;Actually the range is expanded into the corresponding list of separated variable and is resubmited, being at most a one level recursive solution.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers from Portugal.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 22 Oct 2012 10:35:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102045#M21352</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2012-10-22T10:35:27Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102046#M21353</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;@DanielSantos: sas macro does not have the lexical scope. thus no need to nest a macro definition inside the other. This case does not require calling the main function recursively either. Checking for a dash and "un-dashing" if necessary should be enough.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: Consolas, Courier New; font-size: 90%; line-height: 1.1;"&gt;&lt;SPAN&gt;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*-- "undash" a dashed list of a v# - v# type --*;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt; undash(dashed);&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%local&lt;/SPAN&gt;&lt;SPAN&gt; prx from prefix1 prefix2 to i;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; prx = ^\s*([_A-Za-z]+)(\d+)\s*[-]\s*([_A-Za-z]+)(\d+)\s*$;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; prefix1 = &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN&gt;(prxchange(s|&amp;amp;prx|$1|, 1, &amp;amp;dashed));&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; from&amp;nbsp;&amp;nbsp;&amp;nbsp; = &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN&gt;(prxchange(s|&amp;amp;prx|$2|, 1, &amp;amp;dashed));&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; prefix2 = &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN&gt;(prxchange(s|&amp;amp;prx|$3|, 1, &amp;amp;dashed));&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; to&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN&gt;(prxchange(s|&amp;amp;prx|$4|, 1, &amp;amp;dashed));&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*-- if something is wrong then return nothing --*;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%if&lt;/SPAN&gt;&lt;SPAN&gt; &amp;amp;prefix1 ^= &amp;amp;prefix2 or &amp;amp;to &amp;lt; &amp;amp;from &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%then&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%return&lt;/SPAN&gt;&lt;SPAN&gt;; &lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%do&lt;/SPAN&gt;&lt;SPAN&gt; i = &amp;amp;from &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%to&lt;/SPAN&gt;&lt;SPAN&gt; &amp;amp;to - &lt;/SPAN&gt;&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;amp;prefix1&amp;amp;i&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %*;&lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%str&lt;/SPAN&gt;&lt;SPAN&gt;( )&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%end&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;amp;prefix1&amp;amp;to&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; undash;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*-- main --*;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt; doOver(varlist);&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%if&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%index&lt;/SPAN&gt;&lt;SPAN&gt;(&amp;amp;varlist,-) &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%then&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; varlist = %undash(&amp;amp;varlist);&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%if&lt;/SPAN&gt;&lt;SPAN&gt; &amp;amp;varlist= &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%then&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%return&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%local&lt;/SPAN&gt;&lt;SPAN&gt; i n var;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; n = &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN&gt;(countc(&amp;amp;varlist, &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%str&lt;/SPAN&gt;&lt;SPAN&gt;( ))) + 1;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%do&lt;/SPAN&gt;&lt;SPAN&gt; i = &lt;/SPAN&gt;&lt;SPAN style="color: #008080;"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%to&lt;/SPAN&gt;&lt;SPAN&gt; &amp;amp;n;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%let&lt;/SPAN&gt;&lt;SPAN&gt; var = &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%scan&lt;/SPAN&gt;&lt;SPAN&gt;(&amp;amp;varlist, &amp;amp;i, &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%str&lt;/SPAN&gt;&lt;SPAN&gt;( ));&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*;&lt;/SPAN&gt;&lt;SPAN&gt;%&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;SPAN&gt;exec&lt;/SPAN&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;SPAN&gt;(&amp;amp;var)&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%end&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; doOver;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*-- check --*;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*-- this is our macro to call with each var --*;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt; exec(var);&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff;"&gt;%put&lt;/SPAN&gt;&lt;SPAN&gt; do something with &amp;amp;var;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; exec;&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; %&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;SPAN&gt;doOver&lt;/SPAN&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;SPAN&gt;(a1-a3)&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; %&lt;/SPAN&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;SPAN&gt;doOver&lt;/SPAN&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;SPAN&gt;(b1 b3 b5)&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; %&lt;/SPAN&gt;&lt;SPAN style="color: #008000;"&gt;*-- on log&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN style="color: #008000;"&gt;&amp;nbsp; do something with a1&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN style="color: #008000;"&gt;&amp;nbsp; do something with a2&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN style="color: #008000;"&gt;&amp;nbsp; do something with a3&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt; &lt;SPAN style="color: #008000;"&gt;&amp;nbsp; do something with b1&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN style="color: #008000;"&gt;&amp;nbsp; do something with b3&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN style="color: #008000;"&gt;&amp;nbsp; do something with b5&lt;/SPAN&gt;&lt;BR /&gt; &lt;SPAN style="color: #008000;"&gt;&amp;nbsp; --*;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 22 Oct 2012 22:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102046#M21353</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2012-10-22T22:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102047#M21354</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Chang, thanks for the reply but...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Non nesting the macro would imply renaming all invocations of %macr to %doOver (your suggestion), while nesting it will allow you to keep the original name. That's really why I've nested the macro and renamed the parent to the original name.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As for the recursiveness, I believe what I've showed can't be considered recursive, as there is no recursive feedback from the macro to the previous iteration. But I get your point, self-calling the macro just obfuscates the coding, which I really don't like it, also it reminded me what was my mindset at the beginning which I guess I lost on the way. Thank you for pointing that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So here's the "revisited" code, shorter, polished and truly recursive which allows any form of mixed var list and ranges: (&amp;lt;X1 X2 ... XN&amp;gt;|&amp;lt;Y1-YN&amp;gt;)*&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; macr(VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* expands range recursively;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; expand(VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%if %index(%str(&amp;amp;VARLIST),%str(-)) %then %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%let PRX=%sysfunc(prxparse(s!(([_A-Z]+\d+\s+)*)([_A-Z]+)(\d+)[-]{1}([_A-Z]+)(\d+)!$3 $4 $6|$1!i));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%let PRX=%sysfunc(prxchange(&amp;amp;PRX,1,&amp;amp;VARLIST));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; VARLIST;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%do _I=%scan(&amp;amp;PRX,2) %to %scan(&amp;amp;PRX,3);%scan(&amp;amp;PRX,1)&amp;amp;_I%str( )%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; VARLIST;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%sysfunc(compbl(%VARLIST%expand(%scan(&amp;amp;PRX,2,%str(|)))))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%else %str(&amp;amp;VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; expand;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* single macro var code here;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;macro&lt;/STRONG&gt; _macr(VAR);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%put _macr(&amp;amp;VAR);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; _macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* single macro var code here;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%let VARLIST=%expand(&amp;amp;VARLIST);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%do _I=1 %to %sysfunc(countc(&amp;amp;VARLIST,%str( )))+1; * iterate through var list;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%_macr(%scan(%str(&amp;amp;VARLIST),&amp;amp;_I,%str( )));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%&lt;STRONG&gt;mend&lt;/STRONG&gt; macr;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;* so now, everything is possible;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(&lt;STRONG&gt;X1-X10&lt;/STRONG&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(&lt;STRONG&gt;X1-X10&lt;/STRONG&gt; &lt;STRONG&gt;Y1-Y5&lt;/STRONG&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(&lt;STRONG&gt;X1-X10&lt;/STRONG&gt; Y1 Y3 Y6 Y8 Y9);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(X1 X3 X6 X8 X10 &lt;STRONG&gt;Y1-Y5&lt;/STRONG&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(X1 X4 &lt;STRONG&gt;Y1-Y5&lt;/STRONG&gt; X5 X10);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;%macr(X1 X3 X4 X7 X10);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Thanks for the reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers from Portugal.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Oct 2012 09:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Repeat-a-macro/m-p/102047#M21354</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2012-10-25T09:55:06Z</dc:date>
    </item>
  </channel>
</rss>

