<?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 Macro loop over dates in character format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761148#M240800</link>
    <description>&lt;P&gt;I have a number of datasets of the form:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example_201812&lt;/P&gt;&lt;P&gt;example_201901&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;example 202008&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create a macro where the argument is a starting month of the form eg 201812, and the macro loops over a 12 month period starting with the starting month (eg 201812 to 201911). I have something like this in mind:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO year(YYYYMM);

%do j = 0 %to 11;
	%let date_&amp;amp;j. = intnx('months',&amp;amp;YYYYMM.,&amp;amp;j.);

  	%if date_&amp;amp;j.= &amp;amp;YYYYMM. %then %do; /*First month*/

 	proc sql;
	create table WANT_&amp;amp;j. as select
	*
	from example_&amp;amp;date_&amp;amp;j.;
	run;

	%end;
	%else %do; /* Remaining months */

 	proc sql;
	create table WANT_&amp;amp;j. as select
	*
	from example_&amp;amp;date_&amp;amp;j.;
	run;

  	%end;

%MEND;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help would be greatly appreciated&lt;/P&gt;</description>
    <pubDate>Thu, 12 Aug 2021 14:13:08 GMT</pubDate>
    <dc:creator>ChristianWI</dc:creator>
    <dc:date>2021-08-12T14:13:08Z</dc:date>
    <item>
      <title>Macro loop over dates in character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761148#M240800</link>
      <description>&lt;P&gt;I have a number of datasets of the form:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example_201812&lt;/P&gt;&lt;P&gt;example_201901&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;example 202008&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create a macro where the argument is a starting month of the form eg 201812, and the macro loops over a 12 month period starting with the starting month (eg 201812 to 201911). I have something like this in mind:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO year(YYYYMM);

%do j = 0 %to 11;
	%let date_&amp;amp;j. = intnx('months',&amp;amp;YYYYMM.,&amp;amp;j.);

  	%if date_&amp;amp;j.= &amp;amp;YYYYMM. %then %do; /*First month*/

 	proc sql;
	create table WANT_&amp;amp;j. as select
	*
	from example_&amp;amp;date_&amp;amp;j.;
	run;

	%end;
	%else %do; /* Remaining months */

 	proc sql;
	create table WANT_&amp;amp;j. as select
	*
	from example_&amp;amp;date_&amp;amp;j.;
	run;

  	%end;

%MEND;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help would be greatly appreciated&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 14:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761148#M240800</guid>
      <dc:creator>ChristianWI</dc:creator>
      <dc:date>2021-08-12T14:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop over dates in character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761152#M240801</link>
      <description>&lt;P&gt;Tip: work with actual SAS date values so that you can use built in SAS functions and built in SAS formats, instead of character strings which you then have to pull apart yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Partial example (UNTESTED)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis;
%let start_date=%sysevalf('01DEC2018'd);
%do j=0 %to 11;
     %let thismonth=%sysfunc(intnx(month,&amp;amp;start_date,&amp;amp;j,b);
      proc sql;
                ... whatever ...
                from example_%sysfunc(putn(&amp;amp;thismonth,yymmn6.));
       quit;
%end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, macro variable &amp;amp;START_DATE is an actual SAS date value (it equals 21519, which is the number of days since 01JAN1960) and then everything works smoothly using SAS date functions (INTNX) and SAS date formats (YYMMN6.)&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 14:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761152#M240801</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-12T14:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop over dates in character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761182#M240815</link>
      <description>&lt;P&gt;And just a note that if you're trying to append these data sets or combine them somehow, you can use shortcut references.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combined;
set example_2018:  example_2019: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will append all data sets that start with example_2018 or example_2019.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a reference that illustrates how to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 15:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761182#M240815</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-12T15:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop over dates in character format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761326#M240898</link>
      <description>&lt;P&gt;Wow, perfect! Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Aug 2021 01:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-over-dates-in-character-format/m-p/761326#M240898</guid>
      <dc:creator>ChristianWI</dc:creator>
      <dc:date>2021-08-13T01:28:52Z</dc:date>
    </item>
  </channel>
</rss>

