<?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: Run Macro for each month between start month and end month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669359#M200786</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RRR(from, to);
  %local i;
  %do i=&amp;amp;from. %to &amp;amp;to.;
    %if %sysfunc(exist(T&amp;amp;i)) %then %do;
      proc sql;
        create table SUMMARY&amp;amp;i. as
        select sum(X) as SUM_X_&amp;amp;i.	   
        from T&amp;amp;i. ;
      quit;
    %end;
  %end;
%mend RRR;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Jul 2020 04:51:04 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-07-15T04:51:04Z</dc:date>
    <item>
      <title>Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669357#M200784</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's say that I have monthly data sets that are published at my work every month.&lt;/P&gt;
&lt;P&gt;The name of the data sets included form YYMM&lt;/P&gt;
&lt;P&gt;(for example:&lt;/P&gt;
&lt;P&gt;t2005 is for May 2020&lt;/P&gt;
&lt;P&gt;t2004 is for April 2020&lt;/P&gt;
&lt;P&gt;t2003 is for Mar 2020&lt;/P&gt;
&lt;P&gt;and so on )&lt;/P&gt;
&lt;P&gt;I have&amp;nbsp; a SAS program that perform statistics calculations on each of the data sets from month&amp;nbsp; 2004 (Apr 2020) till last month.&lt;/P&gt;
&lt;P&gt;Currently I run it in the way that I need to mention each month that I want to run.&lt;/P&gt;
&lt;P&gt;The name of the macro is RRR.&lt;/P&gt;
&lt;P&gt;So if I want to run for months 2004,2005,2006 then I need to run the statements:&lt;/P&gt;
&lt;P&gt;%RRR(2004);&lt;/P&gt;
&lt;P&gt;%RRR(2005);&lt;/P&gt;
&lt;P&gt;%RRR(2006);&lt;/P&gt;
&lt;P&gt;My question is how to run the macros by define 2 macro parameters: FromYYMM&amp;nbsp; ,ToYYMM&lt;/P&gt;
&lt;P&gt;and then run the macro automatically for each month between&amp;nbsp;FromYYMM and&amp;nbsp;ToYYMM&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t2004;
input x;
cards;
1 10
2 20
3 30
;
run;

Data t2005;
input x;
cards;
1 12
2 18
3 40
4 50
;
run;

Data t2006;
input x;
cards;
1 20
4 48
;
run;

/*Current way to run the macro*/
/*Current way to run the macro*/
/*Current way to run the macro*/
%macro RRR(YYMM)
PROC SQL;
	create table summary&amp;amp;YYMM.  as
	select  sum(x) as Sum_x_&amp;amp;YYMM.	   
	from  t&amp;amp;YYMM.
;
QUIT;
%mend RRR;
%RRR(2004);
%RRR(2005);
%RRR(2006);

/*New way to run the macro*/
/*New way to run the macro*/
/*New way to run the macro*/
%let fromYYMM=2004;
%let ToYYMM=2006;
and now I want to run the macro RRR for each month from fromYYMM till ToYYMM
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jul 2020 04:29:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669357#M200784</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-15T04:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669359#M200786</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RRR(from, to);
  %local i;
  %do i=&amp;amp;from. %to &amp;amp;to.;
    %if %sysfunc(exist(T&amp;amp;i)) %then %do;
      proc sql;
        create table SUMMARY&amp;amp;i. as
        select sum(X) as SUM_X_&amp;amp;i.	   
        from T&amp;amp;i. ;
      quit;
    %end;
  %end;
%mend RRR;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 04:51:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669359#M200786</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-15T04:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669360#M200787</link>
      <description>&lt;P&gt;The problem would be easy to solve, if you had just one dataset and date-variable with the data you now have in the dataset names.&lt;/P&gt;
&lt;P&gt;I think that using fromYYMM and ToYYMM as start/end of a loop should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro fromTo(fromYYMM=, ToYYMM=);
	%local yymm;
	%do yymm = &amp;amp;fromYYMM %to &amp;amp;ToYYMM;
		%rrr(&amp;amp;yymm);
	%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jul 2020 05:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669360#M200787</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-07-15T05:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669370#M200792</link>
      <description>&lt;P&gt;Use a data step to build the loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fromYYMM=2004;
%let ToYYMM=2006;

data _null_;
period = input("&amp;amp;fromyymm.",yymmn4.);
do while (period le input("&amp;amp;toyymm.",yymmn4.));
  call execute('%nrstr(%rrr(' !! put(period,yymmn4.) !! '))');
  period = intnx('month',period,1,'b');
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;EM&gt;Insert usual comment about the intelligence (or lack thereof) of using 2-digit years here.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 07:09:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669370#M200792</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-15T07:09:55Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669371#M200793</link>
      <description>&lt;P&gt;&amp;gt;&amp;nbsp;&lt;EM&gt;Insert usual comment&lt;SPAN&gt;&amp;nbsp;...&lt;/SPAN&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;There is an image above, but it only seems to show when opened in a new tab?&lt;/P&gt;
&lt;P&gt;I can't see it.&amp;nbsp; Who can?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 07:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669371#M200793</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-15T07:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669376#M200797</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;gt;&amp;nbsp;&lt;EM&gt;Insert usual comment&lt;SPAN&gt;&amp;nbsp;...&lt;/SPAN&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;There is an image above, but it only seems to show when opened in a new tab?&lt;/P&gt;
&lt;P&gt;I can't see it.&amp;nbsp; Who can?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you mean the "thumbs up"? I could see that immediately when viewing the message (Safari, MacOSX).&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 07:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669376#M200797</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-15T07:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669427#M200817</link>
      <description>&lt;P&gt;The code suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;is quite elegant and simple, and it may work when you report over more than one year (except that it may also create a summary table for e.g. t1932, if such a table exists, and you go from 2019 into 2020) . But if you expect the tables to exist, and will rather have an error message when one of them does not, this is an alternative possibility:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RRR(from,to);
  %local yymm;
  %do yymm=&amp;amp;from %to &amp;amp;to;
    %if %substr(&amp;amp;yymm,3,2)=13 %then /* jump to first month of next year */
      %let yymm=%eval(&amp;amp;yymm+88);
    PROC SQL;
	  create table summary&amp;amp;YYMM.  as
	  select  sum(x) as Sum_x_&amp;amp;YYMM.	   
	  from  t&amp;amp;YYMM.
      ;
    QUIT;
    %end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;An alternative solution is to collect all the data in one table, and use YYMM as a categorical variable, here is a simple way of doing that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all;
  set work.t: indsname=indata;
  length yymm $4;
  yymm=substr(indata,7,4); /* length of name of library + 3 = 7 */
run;

PROC SQL;
	create table summary  as
	select yymm, sum(x) as Sum_x	   
	from  all
	group by yymm
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;You can change the SET statement to e.g. "&lt;CODE class=" language-sas"&gt;set work.t19: work.t20: indsname=indata;&lt;/CODE&gt;" if you only want the sums for those two years.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 12:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669427#M200817</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-07-15T12:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669433#M200820</link>
      <description>&lt;P&gt;Convert your YYMM strings into dates and then you can use INTNX() to sequence. Use INTCK() to count the number of months.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop(start,end);
%local offset sdate edate yymm;
%let sdate=%sysfunc(inputn(&amp;amp;start.01,yymmdd6.));
%let edate=%sysfunc(inputn(&amp;amp;end.01,yymmdd6.));
%do offset=0 %to %sysfunc(intck(month,&amp;amp;sdate,&amp;amp;edate));
  %let yymm=%sysfunc(intnx(month,&amp;amp;sdate,&amp;amp;offset),yymmn4);
  %rrr(&amp;amp;yymm)
%end;
%mend loop;
%loop(2004,2006);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jul 2020 12:47:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669433#M200820</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-15T12:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669548#M200868</link>
      <description>Thank you but no.&lt;BR /&gt;As you can see there should be use of %let.&lt;BR /&gt;%let fromYYMM=2004;&lt;BR /&gt;%let ToYYMM=2006;&lt;BR /&gt;and then Running of the macro should be done automatically for each month between fromYYMM and ToYYMM</description>
      <pubDate>Wed, 15 Jul 2020 16:01:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669548#M200868</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-15T16:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669553#M200871</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 16:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669553#M200871</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-07-15T16:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: Run Macro for each month between start month and end month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669720#M200933</link>
      <description>&lt;P&gt;&amp;gt;&amp;nbsp;I could see that immediately&lt;/P&gt;
&lt;P&gt;My browser is playing tricks on me. Thanks Kurt.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 364px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47220i1B0E0E44A23B5765/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 20:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-Macro-for-each-month-between-start-month-and-end-month/m-p/669720#M200933</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-15T20:59:12Z</dc:date>
    </item>
  </channel>
</rss>

