<?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 sql loop - from current month generate the next 3 months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438108#M109206</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop_demo(n=);

%do i=0 %to %eval(&amp;amp;n-1);

    data _null_;
        %*increment date;
        date = intnx('month', &amp;amp;cmbtd, &amp;amp;i, 'b');;
        call symputx('yearWant', year(date));
        call symputx('monthWant', month(date));
    run;

%put Year = &amp;amp;yearWant;
%put Month = &amp;amp;monthWant;

%end;
%mend;

%loop_demo(n=4);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 16 Feb 2018 23:37:39 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-02-16T23:37:39Z</dc:date>
    <item>
      <title>macro sql loop - from current month generate the next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438035#M109176</link>
      <description>&lt;P&gt;NEED HELP asap very new to SAS - trying to get the next 3 months to display from current month.&lt;/P&gt;&lt;P&gt;i.e. if current month is September then the next 3 months will be October, November and December but all I was able to find online was how to generate the past 3 months - how can I using the same logic get future months?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;BR /&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-12-01 00:00:00';
%let ENDRep2='2017-12-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;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;          
    
 
PROC SQL;
CONNECT TO TERADATA(USER="USERI" PASSWORD="PASSWORD" SERVER="X" mode=teradata 
DATABASE="X");    

CREATE TABLE  B_CalendarCycleData&amp;amp;i AS
SELECT 
a.TRANSACTION_TYPE,
a.FEATURE_CODE,
put(a.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;lt;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,
e1.gl_DESC1,

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=12 then put(a.cycle_run_year+1, 4.) else put(a.cycle_run_year, 4.) end )
    ||'-'||(case when a.cycle_run_month=12 then '01'
                 when a.cycle_run_month&amp;lt;9 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;lt;9 then '0'||put(a.cycle_run_month+1, 1.0)
                                else put(a.cycle_run_month+1, 2.0) end ) as repMTH ***/

FROM CONNECTION TO TERADATA(
select 
TRANSACTION_TYPE,
FEATURE_CODE,
CYCLE_CODE,
Cycle_Start_Date,
TELCO,
JOURNALIZATION_DATE,
cycle_run_month,
cycle_run_year,
cycle_start_date,
Cycle_End_Date


from GRP_ENH_VW
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.TRANSACTION_TYPE 

DISCONNECT FROM TERADATA;

%END; 
QUIT;
%mend; 
%sqlloop;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 18:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438035#M109176</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-16T18:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: macro sql loop - from current month generate the next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438063#M109186</link>
      <description>&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;lt;&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&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 number"&gt;12&lt;/SPAN&gt;&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 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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your code, what do you think happens when the month is 12? It switches to 13 which isn't valid I suspect.&lt;/P&gt;
&lt;P&gt;Did you look at the SAS macro appendix to loop over dates? It's exactly what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to use INTNX to increment your dates otherwise you&amp;nbsp; have to deal with issues like crossing the year boundaries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And do you have to loop this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 20:26:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438063#M109186</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-16T20:26:32Z</dc:date>
    </item>
    <item>
      <title>Re: macro sql loop - from current month generate the next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438102#M109204</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;,&amp;nbsp;&amp;nbsp;When I do that the query returns with no errors but 0 rows/results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes - want to use the same logic and reverse it to get the next following 3 months.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you for your help!&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 23:16:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438102#M109204</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-16T23:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: macro sql loop - from current month generate the next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438106#M109205</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/191207"&gt;@sufiya&lt;/a&gt;&amp;nbsp;Well, do these future months exist in your Teradata source table?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 23:31:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438106#M109205</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-02-16T23:31:16Z</dc:date>
    </item>
    <item>
      <title>Re: macro sql loop - from current month generate the next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438108#M109206</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop_demo(n=);

%do i=0 %to %eval(&amp;amp;n-1);

    data _null_;
        %*increment date;
        date = intnx('month', &amp;amp;cmbtd, &amp;amp;i, 'b');;
        call symputx('yearWant', year(date));
        call symputx('monthWant', month(date));
    run;

%put Year = &amp;amp;yearWant;
%put Month = &amp;amp;monthWant;

%end;
%mend;

%loop_demo(n=4);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Feb 2018 23:37:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438108#M109206</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-16T23:37:39Z</dc:date>
    </item>
    <item>
      <title>Re: macro sql loop - from current month generate the next 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438257#M109245</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;, yes future data up to the current date of yesterday does exist in teradata.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Feb 2018 15:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-sql-loop-from-current-month-generate-the-next-3-months/m-p/438257#M109245</guid>
      <dc:creator>sufiya</dc:creator>
      <dc:date>2018-02-18T15:18:11Z</dc:date>
    </item>
  </channel>
</rss>

