<?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 do loop to append monthly dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853145#M337226</link>
    <description>&lt;P&gt;To set the start and end use actual dates and not those abbreviations.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mob=%sysfunc(intnx(month,%sysfunc(today()),-16,B));
%let moe=%sysfunc(intnx(month,%sysfunc(today()),-5,E)); 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Generate the abbreviations only when you need to build the dataset names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set 
%do offset=0 %to %sysfunc(intck(month,&amp;amp;mob,&amp;amp;moe));
    cm%sysfunc(intnx(month,&amp;amp;mob,&amp;amp;offset),yymmn6.)
%end;
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 10 Jan 2023 19:49:57 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-01-10T19:49:57Z</dc:date>
    <item>
      <title>macro do loop to append monthly dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853142#M337225</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I am struggling with coming up with the proper do loop to append 12 monthly datasets. I have my begin and end date set up using the macros, would appreciate if someone can help!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below are my begin and end month macros:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let moe=%sysfunc(intnx(month,%sysfunc(today()),-5,same), yymmn.); /*go back 6 mo, begin month*/
%let mob=%sysfunc(intnx(month,%sysfunc(today()),-16,same), yymmn.); /*go back 17 mo, end month*/

%put &amp;amp;mob.;
%put &amp;amp;moe.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for example, the above macros returns mob to be: 2021 Aug and moe to be: 2022 Jul with date format: 202109 to 202208&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All I need is to append datasets between the two dates above by 1 month interval, namely:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cm202109&lt;/P&gt;
&lt;P&gt;cm202110&lt;/P&gt;
&lt;P&gt;cm202111&lt;/P&gt;
&lt;P&gt;cm202112&lt;/P&gt;
&lt;P&gt;cm202201 ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2023 19:41:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853142#M337225</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-01-10T19:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop to append monthly dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853145#M337226</link>
      <description>&lt;P&gt;To set the start and end use actual dates and not those abbreviations.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mob=%sysfunc(intnx(month,%sysfunc(today()),-16,B));
%let moe=%sysfunc(intnx(month,%sysfunc(today()),-5,E)); 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Generate the abbreviations only when you need to build the dataset names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set 
%do offset=0 %to %sysfunc(intck(month,&amp;amp;mob,&amp;amp;moe));
    cm%sysfunc(intnx(month,&amp;amp;mob,&amp;amp;offset),yymmn6.)
%end;
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Jan 2023 19:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853145#M337226</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-10T19:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop to append monthly dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853151#M337227</link>
      <description>&lt;P&gt;Thanks Tom for your quick response!&lt;/P&gt;
&lt;P&gt;I plugged in your code and SAS spit out error saying "do loop is not valid in open code", please see below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    data base;
27           set %do offset=0 %to %sysfunc(intck(month,&amp;amp;mob.,&amp;amp;moe.));
ERROR: The %DO statement is not valid in open code.
28             lcmf.cm%sysfunc(intnx(month,&amp;amp;mob.,&amp;amp;offset),yymmn6.)
WARNING: Apparent symbolic reference OFFSET not resolved.
WARNING: Apparent symbolic reference OFFSET not resolved.
ERROR: Argument 3 to function INTNX referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list.  Execution of %SYSCALL statement or %SYSFUNC 
       or %QSYSFUNC function reference is terminated.
29         %end;
ERROR: The %END statement is not valid in open code.
NOTE 139-205: Line generated by the macro function "SYSFUNC".
28              lcmf.cm.
                ________
                22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, CUROBS, END, INDSNAME, KEY, KEYRESET, KEYS, 
              NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.  

NOTE: Line generated by the macro function "SYSFUNC".
28              lcmf.cm.
                ________
                201
ERROR 201-322: The option is not recognized and will be ignored.

30           ;
31         run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and my code is as below, I only added the libname prefix "lcmf"&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mob=%sysfunc(intnx(month,%sysfunc(today()),-16,B));
%let moe=%sysfunc(intnx(month,%sysfunc(today()),-5,E)); 

%put &amp;amp;mob.;
%put &amp;amp;moe.;

data base;
  set %do offset=0 %to %sysfunc(intck(month,&amp;amp;mob.,&amp;amp;moe.));
    lcmf.cm%sysfunc(intnx(month,&amp;amp;mob.,&amp;amp;offset),yymmn6.)
%end;
  ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any idea of what may go wrong?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2023 20:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853151#M337227</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-01-10T20:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop to append monthly dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853171#M337233</link>
      <description>&lt;P&gt;To use that %DO loop you need to create a macro and then call the macro.&lt;/P&gt;
&lt;P&gt;In that case it might be best to make a macro takes the start and stop dates as inputs and just spits out the list of dataset names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dslist(start,stop);
%local offset;
%do offset=0 %to %sysfunc(intck(month,&amp;amp;start.,&amp;amp;stop.));
  lcmf.cm%sysfunc(intnx(month,&amp;amp;start.,&amp;amp;offset),yymmn6.)
%end;
%mend dslist;

%let mob=%sysfunc(intnx(month,%sysfunc(today()),-16,B));
%let moe=%sysfunc(intnx(month,%sysfunc(today()),-5,E)); 
data want;
  set %dslist(&amp;amp;mob,&amp;amp;moe) ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2023 21:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853171#M337233</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-10T21:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop to append monthly dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853174#M337235</link>
      <description>&lt;P&gt;If you want to create something like the following then no macro code is needed.&lt;/P&gt;
&lt;PRE&gt;data want;
set
cm202109
cm202110
cm202111
cm202112
cm202201
cm202202
cm202203
cm202204
cm202205
cm202206
cm202207
;
run;

&lt;/PRE&gt;
&lt;P&gt;CALL Execute places strings into a execution buffer to start after the end of the data step writing them.&lt;/P&gt;
&lt;PRE&gt;data _null_;
   call execute('data want; set');
   date = intnx('month',today(),-16,'b');
   do until ( date=intnx('month',today(),-5,'b') );
      str=cats('cm',put(date,yymmn.));
      call execute (str );
      date = intnx('month',date,1,'b');
   end;
   /* end the data step. first ; ends the SET statement
   second ends the run*/
   call execute (';  run;');
run;&lt;/PRE&gt;
&lt;P&gt;Text in quotes is literal. Variables resolve the value when written to the buffer.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2023 21:56:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853174#M337235</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-01-10T21:56:50Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop to append monthly dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853230#M337245</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
If you just want append these tables,
that would be easy.
*/

data 
cm202109
cm202110
cm202111
cm202112
cm202201
cm202202
cm202203
cm202204
cm202205
cm202206
cm202207
;
set sashelp.class;
run;


options nodsnferr;
data want;
 set cm202109-cm202207;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Jan 2023 11:44:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/853230#M337245</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-01-11T11:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop to append monthly dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/856024#M338253</link>
      <description>&lt;P&gt;Sorry for my delayed response on this last thread, and thank you so much, Tom! This works like a magic.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 17:15:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-to-append-monthly-dataset/m-p/856024#M338253</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-01-27T17:15:46Z</dc:date>
    </item>
  </channel>
</rss>

