<?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: Macro do loop vars not resolved in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596596#M171799</link>
    <description>Macro variables are local within the macro and dont' exist once the macro is done. I would suggest doing this in a data step and using CALL SYMPUTX() which allows a third parameter that you can specify as scope, local or global.&lt;BR /&gt;</description>
    <pubDate>Tue, 15 Oct 2019 17:22:58 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-10-15T17:22:58Z</dc:date>
    <item>
      <title>Macro do loop vars not resolved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596593#M171797</link>
      <description>&lt;P&gt;Hello, I'm attempting to do a do loop within a macro to create month variables as you can see, however it is telling me the "symbolic reference is not resolved" when I try to %put the values to the log.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro my_month_loop;

data _null_;
%do i=1 %to 12;
%let month_&amp;amp;i = %unquote(%str(%')%sysfunc(intnx(month,%sysfunc(today()),-&amp;amp;i,beginning),yymmddd10.)%str(%'));
%end;
run;

%mend my_month_loop;

%put &amp;amp;month_1;
%put &amp;amp;month_2;
%put &amp;amp;month_3;
%put &amp;amp;month_4;
%put &amp;amp;month_5;
%put &amp;amp;month_6;
%put &amp;amp;month_7;
%put &amp;amp;month_8;
%put &amp;amp;month_9;
%put &amp;amp;month_10;
%put &amp;amp;month_11;
%put &amp;amp;month_12;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 17:17:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596593#M171797</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-10-15T17:17:25Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop vars not resolved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596596#M171799</link>
      <description>Macro variables are local within the macro and dont' exist once the macro is done. I would suggest doing this in a data step and using CALL SYMPUTX() which allows a third parameter that you can specify as scope, local or global.&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Oct 2019 17:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596596#M171799</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-15T17:22:58Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop vars not resolved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596597#M171800</link>
      <description>&lt;P&gt;When put statements attempt to resolve a local macrovariable list, the put statements should be placed within a macro definition. So try executing the put statements inside the macro. Also, you do not need data step compiler/datastep to process macro statements&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 17:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596597#M171800</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-15T17:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop vars not resolved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596599#M171802</link>
      <description>&lt;P&gt;Personally, I'd use a data _null_ step instead.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;EDIT: modified to include quotes, since your original had them included.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	start_date=today();

	do i=1 to 12;
		date=intnx('month', start_date, i, 'b');
                call symputx(catx("_", 'month', i), quote(put(date, yymmddd10.), "'"), 'g');
	end;
run;

%put &amp;amp;month_1.;
%put &amp;amp;month_12.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293046"&gt;@Time_Looper47&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello, I'm attempting to do a do loop within a macro to create month variables as you can see, however it is telling me the "symbolic reference is not resolved" when I try to %put the values to the log.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro my_month_loop;

data _null_;
%do i=1 %to 12;
%let month_&amp;amp;i = %unquote(%str(%')%sysfunc(intnx(month,%sysfunc(today()),-&amp;amp;i,beginning),yymmddd10.)%str(%'));
%end;
run;

%mend my_month_loop;

%put &amp;amp;month_1;
%put &amp;amp;month_2;
%put &amp;amp;month_3;
%put &amp;amp;month_4;
%put &amp;amp;month_5;
%put &amp;amp;month_6;
%put &amp;amp;month_7;
%put &amp;amp;month_8;
%put &amp;amp;month_9;
%put &amp;amp;month_10;
%put &amp;amp;month_11;
%put &amp;amp;month_12;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 17:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596599#M171802</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-15T17:29:17Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop vars not resolved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596618#M171807</link>
      <description>&lt;P&gt;Two problems.&amp;nbsp; First you never ran the macro.&amp;nbsp; Second you didn't make the macro variables global (or create them before running the macro) so they are gone after the macro finishes.&amp;nbsp; Also there is one bit on total non-sense.&amp;nbsp; Why do you have a DATA step wrapped around the %DO loop in your macro definition?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro my_month_loop;
%local i;
%do i=1 %to 12;
  %global month_&amp;amp;i;
  %let month_&amp;amp;i = %unquote(%str(%')%sysfunc(intnx(month,%sysfunc(today()),-&amp;amp;i,b),yymmddd10.)%str(%'));
%end;
%mend my_month_loop;

%my_month_loop;
%put &amp;amp;month_1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Oct 2019 18:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596618#M171807</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-15T18:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop vars not resolved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596622#M171809</link>
      <description>&lt;P&gt;Interesting. What if I wanted to start at todays month, and move backwards 12 months - I changed it to -i instead of i to accomplish&amp;nbsp;going backwards&amp;nbsp;however I want it to start with todays month, and then start going back. As if the loop started at i=0 nd then i=-1 and then i=-2, and so on. So I want the value of month_1 to be '2019-10-01'.&amp;nbsp;I tried changing the start of the loop to i=0 to 11 but it didn't work.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 18:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596622#M171809</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-10-15T18:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro do loop vars not resolved</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596624#M171810</link>
      <description>&lt;P&gt;Why not loop from 0 to -11 by -1?&lt;/P&gt;
&lt;P&gt;Or make a truth table of the values of the macro variable suffix and the offset number you want to use and figure out if there is mathematical formula to map one to the other.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Suffix Offset
1  0
2 -1
3 -2
...
12 -11&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So looks like OFFSET is 1-SUFFIX.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do suffix=1 to 12;
  call symputx(cats('month_',suffix)
     ,cats("'",put(intnx('month',today(0),1-suffix),yymmdd10.),"'")
  );
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2019 05:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-do-loop-vars-not-resolved/m-p/596624#M171810</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-16T05:31:04Z</dc:date>
    </item>
  </channel>
</rss>

