<?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 macro do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407420#M99282</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to run six, stratified logistic regression models. Those which are sorted/stratified by PA_AC1, PA_AC2 and PA_AC3 have the primary predictor PA_AVG, whereas the predictor for those sorted by NA_AC1, NA_AC2 and NA_AC3 is NA_AVG. This is the (non-working) code I've come up with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro mod(var1, var2);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro nest;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%DO i=1 %to 3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=work.last;&lt;/P&gt;&lt;P&gt;by &amp;amp;&amp;amp;var1&amp;amp;i;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc logistic data=work.last;&lt;/P&gt;&lt;P&gt;model outcome=&amp;amp;&amp;amp;var2;&lt;/P&gt;&lt;P&gt;by &amp;amp;&amp;amp;var1&amp;amp;i;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend nest;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend mod;&lt;/P&gt;&lt;P&gt;%mod (PA_AC, PA_AVG);&lt;/P&gt;&lt;P&gt;%mod (NA_AC, NA_AVG);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Oct 2017 18:34:22 GMT</pubDate>
    <dc:creator>Caetreviop543</dc:creator>
    <dc:date>2017-10-25T18:34:22Z</dc:date>
    <item>
      <title>macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407420#M99282</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to run six, stratified logistic regression models. Those which are sorted/stratified by PA_AC1, PA_AC2 and PA_AC3 have the primary predictor PA_AVG, whereas the predictor for those sorted by NA_AC1, NA_AC2 and NA_AC3 is NA_AVG. This is the (non-working) code I've come up with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro mod(var1, var2);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro nest;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%DO i=1 %to 3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=work.last;&lt;/P&gt;&lt;P&gt;by &amp;amp;&amp;amp;var1&amp;amp;i;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc logistic data=work.last;&lt;/P&gt;&lt;P&gt;model outcome=&amp;amp;&amp;amp;var2;&lt;/P&gt;&lt;P&gt;by &amp;amp;&amp;amp;var1&amp;amp;i;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend nest;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend mod;&lt;/P&gt;&lt;P&gt;%mod (PA_AC, PA_AVG);&lt;/P&gt;&lt;P&gt;%mod (NA_AC, NA_AVG);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 18:34:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407420#M99282</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2017-10-25T18:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407428#M99287</link>
      <description>&lt;P&gt;Make sure to use code insert icons on the editor menu bar to insert text.&lt;/P&gt;
&lt;P&gt;Do NOT nest macro definitions. It does not change a macro's definition&amp;nbsp; in any way to nest the %macro/%mend statements inside of some other macro defintion.&amp;nbsp; You can nest the EXECUTION of macro by calling a macro from within another macro.&lt;/P&gt;
&lt;P&gt;But your program does not appear to actually call the inner macro. In fact it does not appear to need the inner macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mod(var1, var2);
%DO i=1 %to 3;
 
proc sort data=work.last;
by &amp;amp;var1.&amp;amp;i;
run;
 
proc logistic data=work.last;
model outcome=&amp;amp;var2.&amp;amp;i;
by &amp;amp;var1.&amp;amp;i;
run;
 
%end;
 
%mend mod;
%mod (PA_AC, PA_AVG);
%mod (NA_AC, NA_AVG);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will work if you have 4 series of actual dataset variables that have names like PA_AC1 - PA_AC3, PA_AVG1-PA_AVG3, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 18:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407428#M99287</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-25T18:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407442#M99291</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Great, thanks! That's super helpful. Is it always necessary to put a period after the macro variable and before the &amp;amp;i, if doing a do loop within a macro?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Emily&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 19:27:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407442#M99291</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2017-10-25T19:27:48Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407480#M99311</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86015"&gt;@Caetreviop543&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Great, thanks! That's super helpful. Is it always necessary to put a period after the macro variable and before the &amp;amp;i, if doing a do loop within a macro?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Emily&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS will see the period as marking the end of the macro variable name. It is not actually needed in this example since &amp;amp;i could not be part of the macro variable name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But your original program had multiple &amp;amp; in the front and that is significant.&lt;/P&gt;
&lt;P&gt;So if X=FRED and I=1 then both "&amp;amp;X&amp;amp;I" and "&amp;amp;X.&amp;amp;I." will resolve to "FRED1".&lt;/P&gt;
&lt;P&gt;But if you add more &amp;amp; in the front the SAS will re-scan the string. So &amp;amp;&amp;amp;X&amp;amp;I will first convert to &amp;amp;X1 and then SAS will look for a macro variable named X1 to resolve.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1631  %let x=FRED;
1632  %let i=1;
1633  %put '&amp;amp;X&amp;amp;I' = "&amp;amp;X&amp;amp;I";
'&amp;amp;X&amp;amp;I' = "FRED1"
1634  %put '&amp;amp;&amp;amp;X&amp;amp;I' = "&amp;amp;&amp;amp;X&amp;amp;I";
WARNING: Apparent symbolic reference X1 not resolved.
'&amp;amp;&amp;amp;X&amp;amp;I' = "&amp;amp;X1"
&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Oct 2017 21:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407480#M99311</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-25T21:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407556#M99334</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86015"&gt;@Caetreviop543&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Great, thanks! That's super helpful. Is it always necessary to put a period after the macro variable and before the &amp;amp;i, if doing a do loop within a macro?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Emily&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's not always necessary, but it is&amp;nbsp;&lt;EM&gt;never&lt;/EM&gt; wrong. Therefore it is considered good practice to always add it.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 07:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop/m-p/407556#M99334</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-10-26T07:04:56Z</dc:date>
    </item>
  </channel>
</rss>

