<?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 to increment months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645067#M192750</link>
    <description>&lt;P&gt;Unfortunately I'm unable to keep things in one data set due to the nature of the data and analysis being performed. I also don't really have much control over the context of the code. I was asked to automate an existing process involving a more complex version of the query in my example.&lt;/P&gt;</description>
    <pubDate>Mon, 04 May 2020 18:12:04 GMT</pubDate>
    <dc:creator>Ody</dc:creator>
    <dc:date>2020-05-04T18:12:04Z</dc:date>
    <item>
      <title>Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645061#M192748</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've searched though some of the previous forum posts but couldn't find something relevant to my particular need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a query that looks something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table month as
select * from some_table
where date between start_date and end_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My goal is to create a macro that will increment the start/end date for each month in a given range. For example, I want to create a data set for each month in the range of May 1 2017 to September 1 2019, where start_date and end_date are the beginning and end of each month in that range. I've seen some examples using intx() but I'm having trouble applying that to this scenario.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate any pointers. Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 17:49:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645061#M192748</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2020-05-04T17:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645064#M192749</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I want to create a data set for each month ...&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Almost always a poor idea that results in more difficult and time-consuming coding than keeping things in one large data set. If you need to do analysis or do reports for each month, you can use the BY statement to accomplish. This is much less coding, and no macro is needed.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 17:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645064#M192749</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-04T17:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645067#M192750</link>
      <description>&lt;P&gt;Unfortunately I'm unable to keep things in one data set due to the nature of the data and analysis being performed. I also don't really have much control over the context of the code. I was asked to automate an existing process involving a more complex version of the query in my example.&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 18:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645067#M192750</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2020-05-04T18:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645072#M192753</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/29074"&gt;@Ody&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Unfortunately I'm unable to keep things in one data set due to the nature of the data and analysis being performed. I also don't really have much control over the context of the code. I was asked to automate an existing process involving a more complex version of the query in my example.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That brings up a question of are your date values in the data actually SAS date values or not. If not then you'll need to provide very explicit examples of what the start and end dates have to look like.&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 18:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645072#M192753</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-04T18:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645074#M192755</link>
      <description>Thanks for thinking through that though that possible issue but I'm comfortable transforming the data into the appropriate format, I'm just having trouble writing a macro to loop through the months of my date range. I'm really just looking for an example I can use as a frame work to build on.</description>
      <pubDate>Mon, 04 May 2020 18:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645074#M192755</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2020-05-04T18:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645076#M192757</link>
      <description>&lt;P&gt;How big is your source data?&amp;nbsp; If it's not too big, could be a nice use case for dynamic data-splitting via hash tables.&amp;nbsp; If not, there's probably a straight forward call execute approach.&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 18:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645076#M192757</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-05-04T18:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645077#M192758</link>
      <description>&lt;P&gt;Check the macro appendix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n01vuhy8h909xgn16p0x6rddpoj9.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n01vuhy8h909xgn16p0x6rddpoj9.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; %macro date_loop(start,end);
   %let start=%sysfunc(inputn(&amp;amp;start,anydtdte9.));
   %let end=%sysfunc(inputn(&amp;amp;end,anydtdte9.));
   %let dif=%sysfunc(intck(month,&amp;amp;start,&amp;amp;end));
     %do i=0 %to &amp;amp;dif;
      %let date=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i,b),date9.);
      %put &amp;amp;date;
     %end;
   %mend date_loop;

   %date_loop(01jul2015,01feb2016)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 May 2020 18:44:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645077#M192758</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-04T18:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645079#M192759</link>
      <description>The data source is financial related so it's relatively large. The query pulls data on a per period basis where a period is defined as the start of the month to the end of the month. The data, being financial in nature, needs to be pulled in this manner for analysis. &lt;BR /&gt;&lt;BR /&gt;The current process is absurd. Someone is running this query over and over again, changing the start and end date. Then they copying the data over to excel. When I heard of this I new there was an easier way to loop through the query and export the data. I've done similar things in the past with export data sets but the date range is giving me a headache. Maybe I'm over thinking it... &lt;BR /&gt;&lt;BR /&gt;I thought I'd get some quick ideas here but instead i'm getting a lot more questions than answers.</description>
      <pubDate>Mon, 04 May 2020 19:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645079#M192759</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2020-05-04T19:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645082#M192761</link>
      <description>&lt;P&gt;First step is to get it to work for one specific case. Your example is NOT that.&amp;nbsp; More like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table MAY2019 as
  select * 
  from some_table
  where date between '01MAY2019'd  and '31MAY2019'd
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Second step is to replace the parts that change with macro variables.&amp;nbsp; To start set the values manually.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsname=MAY2019;
%let start='01MAY2019'd;
%let end='31MAY2019'd;
create table &amp;amp;dsname. as
  select * 
  from some_table
  where date between &amp;amp;start. and &amp;amp;end.
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can start thinking about how to generate a series of these with different values of the macro variables.&lt;/P&gt;
&lt;P&gt;For example if you wanted to do the same thing for each month from MAY2019 to JAN2020 you might to something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let first_date='01MAY2019'd;
%let last_date='01JAN2020'd;
%do offset=0 %to %sysfunc(intck(month,&amp;amp;first_date,&amp;amp;last_date));
  %let start=%sysfunc(intnx(month,&amp;amp;first_date,&amp;amp;offset,b));
  %let end=%sysfunc(intnx(month,&amp;amp;first_date,&amp;amp;offset,e));
  %let dsname=%sysfunc(intnx(month,&amp;amp;first_date,&amp;amp;offset,b),monyy7.);
create table &amp;amp;dsname. as
  select * 
  from some_table
  where date between &amp;amp;start. and &amp;amp;end.
;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 19:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645082#M192761</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-04T19:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645083#M192762</link>
      <description>&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 19:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645083#M192762</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-04T19:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to increment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645084#M192763</link>
      <description>Thanks for the example!</description>
      <pubDate>Mon, 04 May 2020 19:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-increment-months/m-p/645084#M192763</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2020-05-04T19:39:59Z</dc:date>
    </item>
  </channel>
</rss>

