<?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: Macros to loop and append multiple files with year-month name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847137#M334933</link>
    <description>&lt;P&gt;You only need the loop for the dataset names in the SET statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop(start,end);
%let start=%sysfunc(inputn(&amp;amp;start,yymmn6.));
%let end=%sysfunc(inputn(&amp;amp;end,yymmn6.));
%let year=&amp;amp;start;
%let month=&amp;amp;start;
%let incr=0;
data current;
length dname $41;
set
%do %while(&amp;amp;year&amp;lt;=&amp;amp;end);
  %let incr=%eval(&amp;amp;incr+1);
  %put &amp;amp;=incr;
  %let year=%sysfunc(intnx(year,&amp;amp;start,&amp;amp;incr,b));
  %let filename=cm%sysfunc(putn(&amp;amp;year,year4))%sysfunc(month(&amp;amp;start),z2);
  lcmf.&amp;amp;filename. (keep=uci status)
%end;
  indsname=dname
;
source = dname;
run;
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 30 Nov 2022 19:59:06 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-11-30T19:59:06Z</dc:date>
    <item>
      <title>Macros to loop and append multiple files with year-month name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847135#M334932</link>
      <description>&lt;P&gt;Hello, I have been trying to accomplish this task for a while but without luck. I want to grab multiple files and &lt;STRONG&gt;append&lt;/STRONG&gt; them together, and add a new variable to the final dataset indicating the source of each data. Below is my code:&lt;/P&gt;&lt;PRE&gt;%macro loop(start,end);
	%let start=%sysfunc(inputn(&amp;amp;start,yymmn6.));
	%let end=%sysfunc(inputn(&amp;amp;end,yymmn6.));
	%let year=&amp;amp;start;
	%let month=&amp;amp;start;
	%let incr=0;
	%do %while(&amp;amp;year&amp;lt;=&amp;amp;end);
		%let incr=%eval(&amp;amp;incr+1);
		%put &amp;amp;=incr;
		%let year=%sysfunc(intnx(year,&amp;amp;start,&amp;amp;incr,b));
		%let filename=cm%sysfunc(putn(&amp;amp;year,year4))%sysfunc(month(&amp;amp;start),z2);
		data current; set lcmf.&amp;amp;filename.(keep=uci status); 
			source="&amp;amp;filename."; 
		output;
	%end;
	run;
%mend;

%loop(201202,201402)&lt;/PRE&gt;&lt;P&gt;However, the final dataset only contains the last dataset in the loop although the log shows SAS is grabbing each file in the loop, but the loop replaces the previous dataset with the latter one, which is not something I want. The new variable "source" is also incorrect. Basically I want a stacked dataset with datasets: cm201202, cm201302, cm201402 combined with a new variable "source" indicating the year-month information.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help!!!! Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 19:48:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847135#M334932</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2022-11-30T19:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macros to loop and append multiple files with year-month name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847137#M334933</link>
      <description>&lt;P&gt;You only need the loop for the dataset names in the SET statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop(start,end);
%let start=%sysfunc(inputn(&amp;amp;start,yymmn6.));
%let end=%sysfunc(inputn(&amp;amp;end,yymmn6.));
%let year=&amp;amp;start;
%let month=&amp;amp;start;
%let incr=0;
data current;
length dname $41;
set
%do %while(&amp;amp;year&amp;lt;=&amp;amp;end);
  %let incr=%eval(&amp;amp;incr+1);
  %put &amp;amp;=incr;
  %let year=%sysfunc(intnx(year,&amp;amp;start,&amp;amp;incr,b));
  %let filename=cm%sysfunc(putn(&amp;amp;year,year4))%sysfunc(month(&amp;amp;start),z2);
  lcmf.&amp;amp;filename. (keep=uci status)
%end;
  indsname=dname
;
source = dname;
run;
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Nov 2022 19:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847137#M334933</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-11-30T19:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macros to loop and append multiple files with year-month name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847139#M334935</link>
      <description>&lt;P&gt;Thanks Kurt!! Your revision works perfectly except for the new "source" variable. Somehow it shifted by 1 value: currently it shows "cm201302", "cm201402" and "cm201502" instead of "cm201202", "cm201302" and "cm201402", which are the correct values I expect. I know this has something to do with the filename loop set up, do you know any quick fix of this? I actually only want the year and month info, so values like 201202, 201302, 201402 would work as well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 20:15:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847139#M334935</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2022-11-30T20:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macros to loop and append multiple files with year-month name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847142#M334937</link>
      <description>&lt;P&gt;Please post the code&amp;nbsp;&lt;EM&gt;you&lt;/EM&gt; ran.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Nov 2022 20:29:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847142#M334937</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-11-30T20:29:08Z</dc:date>
    </item>
    <item>
      <title>Re: Macros to loop and append multiple files with year-month name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847145#M334939</link>
      <description>&lt;P&gt;exactly the one you replied to above, I also noticed the data files SAS was grabbing are wrong, instead of grabbing cm201202, cm201302, cm201402, it's grabbing cm201302, cm201402, and cm201502. Is there something that needs to be revised to the line "%let incr=%eval(&amp;amp;incr+1);"? Welcome your thoughts.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro loop(start,end);
%let start=%sysfunc(inputn(&amp;amp;start,yymmn6.));
%let end=%sysfunc(inputn(&amp;amp;end,yymmn6.));
%let year=&amp;amp;start;
%let month=&amp;amp;start;
%let incr=0;
data current;
length dname $41;
set
%do %while(&amp;amp;year&amp;lt;=&amp;amp;end);
  %let incr=%eval(&amp;amp;incr+1);
  %put &amp;amp;=incr;
  %let year=%sysfunc(intnx(year,&amp;amp;start,&amp;amp;incr,b));
  %let filename=cm%sysfunc(putn(&amp;amp;year,year4))%sysfunc(month(&amp;amp;start),z2);
  lcmf.&amp;amp;filename. (keep=uci status)
%end;
  indsname=dname;
	source = dname;
run;
%mend;
%loop(201202,201402)&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Nov 2022 20:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847145#M334939</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2022-11-30T20:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macros to loop and append multiple files with year-month name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847147#M334941</link>
      <description>I just thought of a quick fix, which is to shift the start and end date backward by 1, so that SAS is grabbing the correct years I need. No need to tweak the code itself.</description>
      <pubDate>Wed, 30 Nov 2022 20:47:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847147#M334941</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2022-11-30T20:47:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macros to loop and append multiple files with year-month name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847150#M334944</link>
      <description>&lt;P&gt;It's a logical mistake I copied from your code. The incrementation statement needs to be moved:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do %while(&amp;amp;year&amp;lt;=&amp;amp;end);
  %put &amp;amp;=incr;
  %let year=%sysfunc(intnx(year,&amp;amp;start,&amp;amp;incr,b));
  %let filename=cm%sysfunc(putn(&amp;amp;year,year4))%sysfunc(month(&amp;amp;start),z2);
  lcmf.&amp;amp;filename. (keep=uci status)
  %let incr=%eval(&amp;amp;incr+1);
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Nov 2022 20:52:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macros-to-loop-and-append-multiple-files-with-year-month-name/m-p/847150#M334944</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-11-30T20:52:30Z</dc:date>
    </item>
  </channel>
</rss>

