<?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 and loop for multiple months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676191#M203853</link>
    <description>&lt;P&gt;One style choice if must place data in the data set name then place it at the end of the data set instead of in the middle. And then instead of Nov18 use Year&amp;nbsp; and month numbers.&lt;/P&gt;
&lt;P&gt;Instead of Record_&lt;FONT color="#FF0000"&gt;Nov18&lt;/FONT&gt;_Time_All&lt;/P&gt;
&lt;P&gt;use Record_Time_All_201811.&lt;/P&gt;
&lt;P&gt;At least that way your data sets will sort in a reasonable manner and you can use lists for some activities such as&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set Record_Time_All_2018: ;&lt;/P&gt;
&lt;P&gt;to append all of the data sets from 2018 at one time.&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;set Record_Time_All_201801 - Record_Time_All_201804 ;&lt;/P&gt;
&lt;P&gt;to append Jan through Apr data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise sort order would have Apr as the first month and you would have to type out the complete name in desired order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, to clarify your question, you want to split up two annual data sets into months for the sole purpose of sticking them back together into a single set?&lt;/P&gt;
&lt;P&gt;If so then&lt;/P&gt;
&lt;PRE&gt;data Final_Information_Time_Al;
   set 
      AC.INFO_V_2018 (keep = MONTH RECORD_NUMBER TIME_ON_RECORD ) 
      AC.INFO_V_2019 (keep = MONTH RECORD_NUMBER TIME_ON_RECORD ) 
   ; 

   where ("01Nov2018"d&amp;lt;=MONTH&amp;lt;="30Mar2019"d) and 
       TIME_ON_RECORD in (0,1) ;
   RECORD_ID = input (RECORD_NUMBER,12.)
run;&lt;/PRE&gt;
&lt;P&gt;It helps to post code in a code box opened on the forum with either the &amp;lt;/&amp;gt; or running man icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change two dates above an you get period you want. No loops. No multiple sorts.&lt;/P&gt;
&lt;P&gt;Your shown code actually has no reason for the sorts before the supposed combination of the data as you aren't showing any use of the record_number.&lt;/P&gt;</description>
    <pubDate>Wed, 12 Aug 2020 15:04:32 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-08-12T15:04:32Z</dc:date>
    <item>
      <title>Macro and loop for multiple months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676153#M203832</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would it be possible for someone to help me with the code required to produce the macros for the months Nov18-Mar19, and there is also a macro that determines whether it is AC.INFO_V_&lt;FONT color="#FF0000"&gt;2018 &lt;FONT color="#000000"&gt;or&amp;nbsp;AC.INFO_V_&lt;/FONT&gt;2019 &lt;FONT color="#000000"&gt;depending on whether NOV18, DEC18, JAN19, FEB19 or MAR19 is run&lt;/FONT&gt;. &lt;FONT color="#000000"&gt;The code highlighted in red below would ideally be a macro, and then the code would &lt;U&gt;loop&lt;/U&gt; the run in order to get "mmmyy_Time_All", with the &lt;U&gt;five months appended&lt;/U&gt; to create the final "Final_Information_Time_All" datset that I can then merge by RECORD_ID to another dataset.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The "MONTH" variable is &lt;U&gt;numeric&lt;/U&gt;, format: MONYY7. e.g. NOV2018, JAN2019.&lt;/P&gt;&lt;P&gt;The "RECORD_NUMBER" variable is &lt;U&gt;character&lt;/U&gt;, format: $30.&lt;/P&gt;&lt;P&gt;The "TIME_ON_RECORD" variable is &lt;U&gt;numeric&lt;/U&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Current code:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data Record_&lt;FONT color="#FF0000"&gt;Nov18&lt;/FONT&gt;_Time_All;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set AC.INFO_V_&lt;FONT color="#FF0000"&gt;2018&lt;/FONT&gt; (keep = MONTH&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; RECORD_NUMBER&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TIME_ON_RECORD ) ;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;where (&lt;FONT color="#FF0000"&gt;"01Nov2018"d&lt;/FONT&gt;&amp;lt;=MONTH&amp;lt;=&lt;FONT color="#FF0000"&gt;"30Nov2018"d&lt;/FONT&gt;) and TIME_ON_RECORD in (0,1) ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data =&amp;nbsp;Record_&lt;FONT color="#FF0000"&gt;Nov18&lt;/FONT&gt;_Time_All&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;out = &lt;FONT color="#FF0000"&gt;Nov18&lt;/FONT&gt;_Time_All ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;by&amp;nbsp;RECORD_NUMBER ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Final_Information_Time_All ;&amp;nbsp; /*After loop for Nov18-Mar19, this will &lt;U&gt;append all the months together&lt;/U&gt;*/&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set&amp;nbsp; &lt;FONT color="#FF0000"&gt;Nov18&lt;/FONT&gt;_Time_All ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;by&amp;nbsp;RECORD_NUMBER ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;RECORD_ID = input (RECORD_NUMBER,12.) ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data = Final_Information_Time_All ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;by&amp;nbsp;RECORD_ID ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 13:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676153#M203832</guid>
      <dc:creator>Justin9</dc:creator>
      <dc:date>2020-08-12T13:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro and loop for multiple months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676191#M203853</link>
      <description>&lt;P&gt;One style choice if must place data in the data set name then place it at the end of the data set instead of in the middle. And then instead of Nov18 use Year&amp;nbsp; and month numbers.&lt;/P&gt;
&lt;P&gt;Instead of Record_&lt;FONT color="#FF0000"&gt;Nov18&lt;/FONT&gt;_Time_All&lt;/P&gt;
&lt;P&gt;use Record_Time_All_201811.&lt;/P&gt;
&lt;P&gt;At least that way your data sets will sort in a reasonable manner and you can use lists for some activities such as&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set Record_Time_All_2018: ;&lt;/P&gt;
&lt;P&gt;to append all of the data sets from 2018 at one time.&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;set Record_Time_All_201801 - Record_Time_All_201804 ;&lt;/P&gt;
&lt;P&gt;to append Jan through Apr data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise sort order would have Apr as the first month and you would have to type out the complete name in desired order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, to clarify your question, you want to split up two annual data sets into months for the sole purpose of sticking them back together into a single set?&lt;/P&gt;
&lt;P&gt;If so then&lt;/P&gt;
&lt;PRE&gt;data Final_Information_Time_Al;
   set 
      AC.INFO_V_2018 (keep = MONTH RECORD_NUMBER TIME_ON_RECORD ) 
      AC.INFO_V_2019 (keep = MONTH RECORD_NUMBER TIME_ON_RECORD ) 
   ; 

   where ("01Nov2018"d&amp;lt;=MONTH&amp;lt;="30Mar2019"d) and 
       TIME_ON_RECORD in (0,1) ;
   RECORD_ID = input (RECORD_NUMBER,12.)
run;&lt;/PRE&gt;
&lt;P&gt;It helps to post code in a code box opened on the forum with either the &amp;lt;/&amp;gt; or running man icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change two dates above an you get period you want. No loops. No multiple sorts.&lt;/P&gt;
&lt;P&gt;Your shown code actually has no reason for the sorts before the supposed combination of the data as you aren't showing any use of the record_number.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 15:04:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676191#M203853</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-12T15:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro and loop for multiple months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676210#M203862</link>
      <description>&lt;P&gt;Thanks a lot for your solution, it makes total sense that I should use the method that you have suggested!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the "Final_Information_Time_All" dataset as the spine dataset, where I want to merge on another dataset (called "BASE_DATA") by RECORD_ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As the "RECORD_NUMBER" will repeat every month in AC.INFO_V_2018 and&amp;nbsp;AC.INFO_V_2019, I would have to &lt;U&gt;sort&amp;nbsp;"Final_Information_Time_All" dataset by MONTH and RECORD_ID&lt;/U&gt; (so that the months are kept in order from Nov18-Mar19). However, after I &lt;U&gt;sort the "BASE_DATA" dataset by RECORD_ID&lt;/U&gt; and try to merge onto&amp;nbsp;"Final_Information_Time_All", I'm guessing that they'll be an error because the&amp;nbsp;"Final_Information_Time_All" dataset by both MONTH and RECORD_ID, so RECORD_ID won't be in order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would the best method be to sort the "Final_Information_Time_All" dataset by both MONTH and RECORD_ID, and sort the "BASE_DATA" dataset by RECORD_ID, then left join on RECORD_ID using proc sql?&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 15:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676210#M203862</guid>
      <dc:creator>Justin9</dc:creator>
      <dc:date>2020-08-12T15:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro and loop for multiple months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676215#M203867</link>
      <description>&lt;P&gt;Let's just ignore whether the steps you want to do are reasonable and concentrate on the question of looping over months.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When looping over months you probably want to use a normal %DO/%TO/%BY loop over offset number and then use INTNX() function to generate the actual date.&amp;nbsp; You can use INTCK() function to figure out the upper bound on your loop.&lt;/P&gt;
&lt;P&gt;So if you want to loop from 01&lt;SPAN&gt;Nov2018 to 01Mar2019 you might set the range with macro variables like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let SDATE=01NOV2018;
%let EDATE=01MAR2019;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Then your looping code will look something like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local offset date monyy yr;
%do offset=0 %to %sysfunc(intck(month,"&amp;amp;sdate"d,"&amp;amp;edate"d));
  %let date=%sysfunc(intnx(month,"&amp;amp;sdate"d,&amp;amp;offset));
  %let monyy = %sysfunc(putn(&amp;amp;date,monyy5.));
  %let yr = %sysfunc(putn(&amp;amp;date,year4.));

data Record_&amp;amp;monyy._Time_All ;
   set AC.INFO_V_&amp;amp;yr.  (...) ; 
   where (&amp;amp;date &amp;lt;=MONTH&amp;lt;= %sysfunc(intnx(month,&amp;amp;date,0,e)))
     and TIME_ON_RECORD in (0,1) 
   ;
...
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Aug 2020 16:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676215#M203867</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-12T16:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro and loop for multiple months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676223#M203872</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Here is a generic macro that loop over month between a start date and an end date.&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;%macro loop_month(start, end, action);
    /* action : name of a macro that takes a date value as parameter */

    %let MAXITER=10;

    data _NULL_;
        start=input("&amp;amp;start.", anydtdte.);
        end=input("&amp;amp;end.", anydtdte.);

        curr=start;

        do while (curr&amp;lt;=end and i&amp;lt;&amp;amp;MAXITER.);
            call execute(cats('%nrstr(%&amp;amp;action.)(', curr, ')'));
            curr=intnx("month", curr, 1);
            i+1;
        end;
    run;

%mend loop_month;


%macro mymacro(date);

    data _NULL_;
        date=&amp;amp;date.;
        call symputx('mmyy', put(date, monyy5.));
        call symputx("first_day_of_month", intnx("month", date, 0, "b"));
        call symputx("last_day_of_month", intnx("month", date, 0, "e"));
    run;

    data test_&amp;amp;mmyy.;
        set sashelp.class;
    run;

    proc sql noprint;
        CREATE TABLE dates_&amp;amp;mmyy. AS
        SELECT date
        FROM dates
        WHERE date BETWEEN &amp;amp;first_day_of_month. AND &amp;amp;last_day_of_month.
        ;
    quit;

%mend mymacro;

data dates;
    input date date9.;
    cards;
    25Jan2017
    30Nov18
    02Feb2019
    20Aug2019
    01Jan2020
    ;
quit;

%loop_month(01Nov2018, 01May2019, mymacro)

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 16:21:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-and-loop-for-multiple-months/m-p/676223#M203872</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-08-12T16:21:49Z</dc:date>
    </item>
  </channel>
</rss>

