<?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 loop to get next 4 months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437664#M109024</link>
    <description>&lt;P&gt;Consider where the %END of the loop is.&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/191207"&gt;@sufiya&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;if I run your query by it self - then yes no errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but when I add the rest of the code as&amp;nbsp;&lt;/P&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>Thu, 15 Feb 2018 16:09:16 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-02-15T16:09:16Z</dc:date>
    <item>
      <title>macro loop to get next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437414#M108903</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;A bit lost here, I am using the logic on how to get previous months - reversing to get next month but it doesnt&amp;nbsp;seem to be working ...if someone can please help....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is to get previous months:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** the start data and end data of the month reported*****/
%let cmbtd='01Sep2017'd;
%let cmetd='30Sep2017'd;

%let repMthCM=09;            /***current month**********/
%let repYrCM=2017;

%let startRep1='2017-09-01 00:00:00';
%let ENDRep1='2017-09-30 00:00:00';


%let startRep2='2017-08-01 00:00:00';
%let ENDRep2='2017-08-31 00:00:00';


/**************************************************************/

/****Loop: ***i=1=current month , i=2 Prevous Month, i=3 Prevous previous Month, i=4 Previous previous previous month****/

%macro sqlloop; 
       PROC SQL; 
         %DO i=1 %TO 4;         
            %if &amp;amp;repMthCM-&amp;amp;i&amp;lt;1 %then %do;
                  %let repMth=12+&amp;amp;repMthCM-&amp;amp;i ;
                   %let repYr=&amp;amp;repYrCM-1;
            %end;

            %else %do;
                    %let repMth=&amp;amp;repMthCM-&amp;amp;i ;
                    %let repYr=&amp;amp;repYrCM;
            %end;    &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and using that to get the next 3 months...&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** the start data and end data of the month reported*****/
%let cmbtd='01SEP2017'd;
%let cmetd='30SEP2017'd;

%let repMthCM=09;            /***current month**********/
%let repYrCM=2017;

%let startRep1='2017-09-01 00:00:00';
%let ENDRep1='2017-09-31 00:00:00';


%let startRep2='2017-10-01 00:00:00';
%let ENDRep2='2017-10-31 00:00:00';


/**************************************************************/
/****Loop: ***i=1=current month , i=2 Next Month, i=3 Next next Month, i=4 Next next next month****/

%macro sqlloop; 
       PROC SQL; 
         %DO i=1 %TO 4;         
            %if &amp;amp;repMthCM+&amp;amp;i&amp;gt;1 %then %do;
                  %let repMth=12+&amp;amp;repMthCM+&amp;amp;i ;
                   %let repYr=&amp;amp;repYrCM+1;
            %end;

            %else %do;
                    %let repMth=&amp;amp;repMthCM+&amp;amp;i ;
                    %let repYr=&amp;amp;repYrCM;
            %end; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 10:48:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437414#M108903</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-15T10:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 4 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437435#M108911</link>
      <description>&lt;P&gt;In the ballpark, but needs to be tested:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;        
            &lt;SPAN class="token macrostatement"&gt;%if&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;repMthCM&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;12&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%then&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
                  &lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; repMth&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;repMthCM&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i-12 &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
                   &lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; repYr&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;repYrCM&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
            &lt;SPAN class="token macrostatement"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

            &lt;SPAN class="token macrostatement"&gt;%else&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
                    &lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; repMth&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;repMthCM&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
                    &lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; repYr&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;repYrCM&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
            &lt;SPAN class="token macrostatement"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Feb 2018 07:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437435#M108911</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-15T07:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 4 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437486#M108936</link>
      <description>&lt;P&gt;You can use intnx function called inside sysfunc for better sollution&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my approach. Let us know it it helped.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let startRep=01Nov2017; /*Used Standard SAS Date formats*/

%macro sqlloop;
  %do i=0 %to 3;
    %let j=%eval(&amp;amp;i+1);
    %let startRep&amp;amp;j=%sysfunc(intnx(month,"&amp;amp;startRep."d,-&amp;amp;i.));
    %put &amp;amp;&amp;amp;startRep&amp;amp;j;
  %end;
%mend sqlloop;
%sqlloop;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Feb 2018 10:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437486#M108936</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2018-02-15T10:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 4 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437587#M108987</link>
      <description>&lt;P&gt;THank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;, I tried your logic and had to changed&amp;nbsp;&amp;nbsp;%if &amp;amp;repMthCM+&amp;amp;i&amp;gt;12 to&amp;nbsp;%if &amp;amp;repMthCM+&amp;amp;i&amp;gt;1, as i want to get the next the next 3 months after current month September then want October, November and December months to show....(sorry for the confusion did not mean the next 4 months).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the code below is generating past months i.e current month September - past months August, July and June showing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/****Loop: ***i=1=current month , i=2 Next Month, i=3 Next next Month, i=4 Next next next month****/

%macro sqlloop; 
       PROC SQL; 
         %DO i=1 %TO 4;         
         %if &amp;amp;repMthCM+&amp;amp;i&amp;gt;1 %then %do;
                  %let repMth=&amp;amp;repMthCM+&amp;amp;i-12 ;
                   %let repYr=&amp;amp;repYrCM+1;
            %end;

            %else %do;
                    %let repMth=&amp;amp;repMthCM+&amp;amp;i ;
                    %let repYr=&amp;amp;repYrCM;
            %end;   &lt;BR /&gt;create table test1 as &lt;BR /&gt;select&lt;BR /&gt;put(a.cycle_run_year, 4.)||'-'||(case when a.cycle_run_month&amp;gt;10 then '0'||put(a.cycle_run_month, 1.0)&lt;BR /&gt; else put(a.cycle_run_month, 2.0) end ) as cycle_run_YR_MTH,&lt;BR /&gt;a.cycle_run_month,&lt;BR /&gt;a.cycle_run_year,&lt;BR /&gt;datepart(a.cycle_start_date) as Cycle_Start_DateO format = date9.,&lt;BR /&gt;DATEPART(a.Cycle_End_Date) AS cycle_END_dateO FORMAT = DATE9.,&lt;BR /&gt;&lt;BR /&gt;(case when a.cycle_run_month=9 then put(a.cycle_run_year+1, 4.) else put(a.cycle_run_year, 4.) end )&lt;BR /&gt; ||'-'||(case when a.cycle_run_month=9 then '01'&lt;BR /&gt; when a.cycle_run_month&amp;gt;9 then '0'||put(a.cycle_run_month+1, 1.0)&lt;BR /&gt; else put(a.cycle_run_month+1, 2.0) end ) as repMTH &lt;BR /&gt; from CalendarSource&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 14:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437587#M108987</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-15T14:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 4 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437603#M108995</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138619"&gt;@Satish_Parida&lt;/a&gt;&amp;nbsp;! when I tried your logic received an error -&amp;gt; ERROR: There is no matching %DO statement for the %END. This statement will be ignored.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 14:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437603#M108995</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-15T14:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 4 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437606#M108997</link>
      <description>Runs fine for me.&lt;BR /&gt;Log for reference.&lt;BR /&gt;&lt;BR /&gt;4 %let startRep=01Nov2017; /*Used Standard SAS Date formats*/&lt;BR /&gt;5&lt;BR /&gt;6 %macro sqlloop;&lt;BR /&gt;7 %do i=0 %to 3;&lt;BR /&gt;8 %let j=%eval(&amp;amp;i+1);&lt;BR /&gt;9 %let startRep&amp;amp;j=%sysfunc(intnx(month,"&amp;amp;startRep."d,-&amp;amp;i.));&lt;BR /&gt;10 %put &amp;amp;&amp;amp;startRep&amp;amp;j;&lt;BR /&gt;11 %end;&lt;BR /&gt;12 %mend sqlloop;&lt;BR /&gt;13 %sqlloop;&lt;BR /&gt;21124&lt;BR /&gt;21093&lt;BR /&gt;21063&lt;BR /&gt;21032</description>
      <pubDate>Thu, 15 Feb 2018 14:53:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437606#M108997</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2018-02-15T14:53:38Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 4 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437629#M109012</link>
      <description>&lt;P&gt;if I run your query by it self - then yes no errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but when I add the rest of the code as&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; startRep&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;01&lt;/SPAN&gt;Nov2017&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token comment"&gt;/*Used Standard SAS Date formats*/&lt;/SPAN&gt;

&lt;SPAN class="token macrobound"&gt;%macro&lt;/SPAN&gt; sqlloop&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt; i&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%to&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
    &lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; j&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token macroname"&gt;%eval&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
    &lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; startRep&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;j&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token macrostatement"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;intnx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;month&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"&amp;amp;startRep."&lt;/SPAN&gt;d&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
    &lt;SPAN class="token macrostatement"&gt;%put&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;startRep&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token macrostatement"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;BR /&gt;&lt;/CODE&gt;PROC SQL; &lt;BR /&gt;CREATE TABLE TEST AS &lt;BR /&gt;&lt;BR /&gt;SELECT &lt;BR /&gt;put(a.CYCLE_CODE, 9.0) as CYCLE_CODE, &lt;BR /&gt;put(day(DATEPART(a.Cycle_Start_Date)), z2.) as Start_Date_IND, &lt;BR /&gt;put(day(DATEPART(a.Cycle_Start_Date)), z2.) as Cycle_Start_Date, &lt;BR /&gt;Cycle_Start_Date as Cycle_Start_Day, &lt;BR /&gt;Animal, &lt;BR /&gt;put(cycle_run_year, 4.)||'-'||(case when a.cycle_run_month&amp;gt;10 then '0'||put(a.cycle_run_month, 1.0) &lt;BR /&gt;else put(cycle_run_month, 2.0) end ) as cycle_run_YR_MTH, &lt;BR /&gt;cycle_run_month, &lt;BR /&gt;cycle_run_year, &lt;BR /&gt;datepart(cycle_start_date) as Cycle_Start_DateO format = date9., &lt;BR /&gt;DATEPART(Cycle_End_Date) AS cycle_END_dateO FORMAT = DATE9., &lt;BR /&gt;(case when cycle_run_month=9 then put(cycle_run_year+1, 4.) &lt;BR /&gt;else put(cycle_run_year, 4.) end ) ||'-'||(case when a.cycle_run_month=9 then '01' &lt;BR /&gt;when cycle_run_month&amp;gt;12 then '0'||put(cycle_run_month+1, 1.0) &lt;BR /&gt;else put(cycle_run_month+1, 2.0) end ) as repMTH /*** put(cycle_run_year, 4.)||'-'||&lt;BR /&gt;(case when cycle_run_month&amp;gt;9 then '0'||put(cycle_run_month+1, 1.0) else put(cycle_run_month+1, 2.0) &lt;BR /&gt;end ) as repMTH ***/ &lt;BR /&gt;&lt;BR /&gt;from calendar.source &lt;BR /&gt;where cycle_run_month =&amp;amp;repMth /&lt;BR /&gt;***The Marco process has make the next month for calendar cycle****/ and cycle_run_year =&amp;amp;repYr &lt;BR /&gt;%END; &lt;BR /&gt;QUIT; &lt;BR /&gt;%mend; &lt;BR /&gt;%sqlloop; &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 15:21:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437629#M109012</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-15T15:21:33Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 4 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437664#M109024</link>
      <description>&lt;P&gt;Consider where the %END of the loop is.&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/191207"&gt;@sufiya&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;if I run your query by it self - then yes no errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but when I add the rest of the code as&amp;nbsp;&lt;/P&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>Thu, 15 Feb 2018 16:09:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437664#M109024</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-15T16:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop to get next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437705#M109034</link>
      <description>&lt;P&gt;would anyone please be able to help and tell what needs to be done? to get the next 3 months October,November, December - if current month is September?&amp;nbsp; for whatever reason i either get August,July,June or January, February data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** the start data and end data of the month reported*****/
%let cmbtd='01Sep2017'd;
%let cmetd='30Sep2017'd;

%let repMthCM=09;            /***current month**********/
%let repYrCM=2017;

%let startRep1='2017-10-01 00:00:00';
%let ENDRep1='2017-10-31 00:00:00';


%let startRep2='2017-10-01 00:00:00';
%let ENDRep2='2017-10-31 00:00:00';

/****Loop: ***i=1=current month , i=2 Next Month, i=3 Next next Month, i=4 Next next next month****/

%macro sqlloop; 
       PROC SQL; 
         %DO i=1 %TO 4;         
         %if &amp;amp;repMthCM+&amp;amp;i&amp;gt;1 %then %do;
                  %let repMth=&amp;amp;repMthCM+&amp;amp;i-12 ;
                   %let repYr=&amp;amp;repYrCM+1;
            %end;

            %else %do;
                    %let repMth=&amp;amp;repMthCM+&amp;amp;i ;
                    %let repYr=&amp;amp;repYrCM;
            %end;  
 
PROC SQL;
CREATE TABLE Calendar AS
SELECT 
TRANSACTION_TYPE,
put(CYCLE_CODE, 9.0) as CYCLE_CODE,
put(day(DATEPART(a.Cycle_Start_Date)), z2.) as Start_Date_IND,
put(day(DATEPART(a.Cycle_Start_Date)), z2.) as Cycle_Start_Date,
a.Cycle_Start_Date as Cycle_Start_Day,
a.JOURNALIZATION_DATE,
put(a.cycle_run_year, 4.)||'-'||(case when a.cycle_run_month&amp;gt;10 then '0'||put(a.cycle_run_month, 1.0)
                                else put(a.cycle_run_month, 2.0) end ) as cycle_run_YR_MTH,
a.cycle_run_month,
a.cycle_run_year,
datepart(a.cycle_start_date) as Cycle_Start_DateO format = date9.,
DATEPART(a.Cycle_End_Date) AS cycle_END_dateO FORMAT = DATE9.,

(case when a.cycle_run_month=9 then put(a.cycle_run_year+1, 4.) else put(a.cycle_run_year, 4.) end )
    ||'-'||(case when a.cycle_run_month=9 then '01'
                 when a.cycle_run_month&amp;gt;12 then '0'||put(a.cycle_run_month+1, 1.0)
                    else put(a.cycle_run_month+1, 2.0) end ) as repMTH 

/***
put(a.cycle_run_year, 4.)||'-'||(case when a.cycle_run_month&amp;gt;9 then '0'||put(a.cycle_run_month+1, 1.0)
                                else put(a.cycle_run_month+1, 2.0) end ) as repMTH ***/

select 
TRANSACTION_TYPE,
CYCLE_CODE,
Cycle_Start_Date,
 
JOURNALIZATION_DATE,
cycle_run_month,
cycle_run_year,
cycle_start_date,
Cycle_End_Date


from  VIEWA
where CYCLE_CODE in (6, 23, 24, 31)
and cycle_run_month =&amp;amp;repMth  /***The Marco proess has make the prior month for caledear cycle****/
and cycle_run_year =&amp;amp;repYr
) as A

left join lm_gl1 e1 on a. TRANSACTION_TYPE = e1.gl 
left join lm_gl2 e2 on a. TRANSACTION_TYPE = e2.ftr 

%END; 
QUIT;
%mend; 
%sqlloop;
&lt;BR /&gt;&lt;BR /&gt;%macro sqlloop; &lt;BR /&gt; PROC SQL; &lt;BR /&gt; %DO i=1 %TO 4; &lt;BR /&gt; %let repMthStart=%sysfunc(dequote("'%sysfunc(intnx(month,&amp;amp;cmbtd, +&amp;amp;i+1, b),yymmdd10.) 00:00:00'"));&lt;BR /&gt; %let repMthEnd=%sysfunc(dequote("'%sysfunc(intnx(month,&amp;amp;cmetd, +&amp;amp;i+1, e),yymmdd10.) 00:00:00'"));&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 17:35:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-to-get-next-3-months/m-p/437705#M109034</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-15T17:35:48Z</dc:date>
    </item>
  </channel>
</rss>

