<?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: looping thru dates dynamically in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885332#M349830</link>
    <description>I thought it was an error on the last line invoking the macro, my mistake.</description>
    <pubDate>Tue, 18 Jul 2023 17:59:41 GMT</pubDate>
    <dc:creator>hporter</dc:creator>
    <dc:date>2023-07-18T17:59:41Z</dc:date>
    <item>
      <title>looping thru dates dynamically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885321#M349821</link>
      <description>&lt;P&gt;I have a dataset with account numbers and archive dates spanning 13 months. I then need to do some analysis on comparing 1 month to the next month. I have this all coded and it works, but I have to manually input the beginning and end dates to do the comparison and I would like to loop through and dynamically generate all the tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically my begin date for the first round needs to be 01JUN2022 and my end date needs to be 01JUL2022, which is represented by this:&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -12), date9.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -11), date9.&lt;SPAN&gt;);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For the 2nd round I need my begin date to be 01JUL2022 and my end date to be 01AUG2022, and so on and so forth thru 01JUL2023.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an edited version of the code to make it as simple as possible:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -12), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -11), yymmddn8.);&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table myTable&lt;BR /&gt;as select&lt;/P&gt;
&lt;P&gt;acctrefno, archive_date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from myTable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where archive_date in (&lt;BR /&gt;'01jul2022'd,&lt;BR /&gt;'01aug2022'd,&lt;BR /&gt;'01sep2022'd,&lt;BR /&gt;'01oct2022'd,&lt;BR /&gt;'01nov2022'd,&lt;BR /&gt;'01dec2022'd,&lt;BR /&gt;'01jan2023'd,&lt;BR /&gt;'01feb2023'd,&lt;BR /&gt;'01mar2023'd,&lt;BR /&gt;'01apr2023'd,&lt;BR /&gt;'01may2023'd,&lt;BR /&gt;'01jun2023'd&lt;BR /&gt;'01jul2023'd)&lt;/P&gt;
&lt;P&gt;order by archive_date&lt;BR /&gt;;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table a&lt;BR /&gt;as select&lt;/P&gt;
&lt;P&gt;acctrefno, archive_date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from work.myTable&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;where archive_date = &amp;amp;beg&lt;BR /&gt;order by acctrefno&lt;BR /&gt;;quit;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table b&lt;BR /&gt;as select&lt;/P&gt;
&lt;P&gt;acctrefno, archive_date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from work.myTable&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;where archive_date = &amp;amp;end&lt;BR /&gt;order by acctrefno&lt;BR /&gt;;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each time I run the code I am switching the&amp;nbsp;&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -12), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -11), yymmddn8.);&amp;nbsp;&lt;BR /&gt;to reflect the next round. So in this case that is round 1. The next would be:&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -11), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -10), yymmddn8.);&lt;BR /&gt;the next would be:&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -10), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -9), yymmddn8.);&lt;BR /&gt;&lt;BR /&gt;Any help to automate this is greatly appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 17:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885321#M349821</guid>
      <dc:creator>hporter</dc:creator>
      <dc:date>2023-07-18T17:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: looping thru dates dynamically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885323#M349822</link>
      <description>&lt;P&gt;A macro loop would work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EXAMPLE: UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis;
    %do i=-12 %to -1;
         %let beg = %sysfunc(intnx(month, %sysfunc(today()), &amp;amp;i), yymmddn8.);
         %let end = %sysfunc(intnx(month, %sysfunc(today()), %eval(&amp;amp;i+1), yymmddn8.); 
/* Any other code you want goes here */
     %end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, usually you would not want to format macro variable values, the unformatted values are what you need. That way you don't have to first format the values and then remove the format for use in code. Although it depends on what you have already coded.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 17:30:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885323#M349822</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-18T17:30:58Z</dc:date>
    </item>
    <item>
      <title>Re: looping thru dates dynamically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885325#M349823</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop;

%do i=-12 %to -1;

%let beg = %sysfunc(intnx(month, %sysfunc(today()), &amp;amp;i., yymmddn8.);
%let end = %sysfunc(intnx(month, %sysfunc(today()), %eval(&amp;amp;.i+1)), yymmddn8.);



proc sql;
create table myTable
as select
acctrefno, archive_date
from myTable
where archive_date in (
'01jul2022'd,
'01aug2022'd,
'01sep2022'd,
'01oct2022'd,
'01nov2022'd,
'01dec2022'd,
'01jan2023'd,
'01feb2023'd,
'01mar2023'd,
'01apr2023'd,
'01may2023'd,
'01jun2023'd
'01jul2023'd)

order by archive_date
;quit;

 

proc sql;
create table a_&amp;amp;i.
as select

acctrefno, archive_date

 

from work.myTable


where archive_date = &amp;amp;beg
order by acctrefno
;quit;


proc sql;
create table b_&amp;amp;i.
as select
acctrefno, archive_date
from work.myTable
where archive_date = &amp;amp;end
order by acctrefno
;quit;

%end;

%mend loop;

%loop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;untested. Are the dates in the SQL hardcoded? I think you could probably do this without macro's in a different way, using group by logic, but depends on the next steps to some degree.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269855"&gt;@hporter&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a dataset with account numbers and archive dates spanning 13 months. I then need to do some analysis on comparing 1 month to the next month. I have this all coded and it works, but I have to manually input the beginning and end dates to do the comparison and I would like to loop through and dynamically generate all the tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically my begin date for the first round needs to be 01JUN2022 and my end date needs to be 01JUL2022, which is represented by this:&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -12), date9.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -11), date9.&lt;SPAN&gt;);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For the 2nd round I need my begin date to be 01JUL2022 and my end date to be 01AUG2022, and so on and so forth thru 01JUL2023.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an edited version of the code to make it as simple as possible:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -12), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -11), yymmddn8.);&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table myTable&lt;BR /&gt;as select&lt;/P&gt;
&lt;P&gt;acctrefno, archive_date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from myTable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where archive_date in (&lt;BR /&gt;'01jul2022'd,&lt;BR /&gt;'01aug2022'd,&lt;BR /&gt;'01sep2022'd,&lt;BR /&gt;'01oct2022'd,&lt;BR /&gt;'01nov2022'd,&lt;BR /&gt;'01dec2022'd,&lt;BR /&gt;'01jan2023'd,&lt;BR /&gt;'01feb2023'd,&lt;BR /&gt;'01mar2023'd,&lt;BR /&gt;'01apr2023'd,&lt;BR /&gt;'01may2023'd,&lt;BR /&gt;'01jun2023'd&lt;BR /&gt;'01jul2023'd)&lt;/P&gt;
&lt;P&gt;order by archive_date&lt;BR /&gt;;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table a&lt;BR /&gt;as select&lt;/P&gt;
&lt;P&gt;acctrefno, archive_date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from work.myTable&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;where archive_date = &amp;amp;beg&lt;BR /&gt;order by acctrefno&lt;BR /&gt;;quit;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table b&lt;BR /&gt;as select&lt;/P&gt;
&lt;P&gt;acctrefno, archive_date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from work.myTable&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;where archive_date = &amp;amp;end&lt;BR /&gt;order by acctrefno&lt;BR /&gt;;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each time I run the code I am switching the&amp;nbsp;&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -12), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -11), yymmddn8.);&amp;nbsp;&lt;BR /&gt;to reflect the next round. So in this case that is round 1. The next would be:&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -11), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -10), yymmddn8.);&lt;BR /&gt;the next would be:&lt;BR /&gt;%let beg = %sysfunc(intnx(month, %sysfunc(today()), -10), yymmddn8.);&lt;BR /&gt;%let end = %sysfunc(intnx(month, %sysfunc(today()), -9), yymmddn8.);&lt;BR /&gt;&lt;BR /&gt;Any help to automate this is greatly appreciated.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 17:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885325#M349823</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-07-18T17:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: looping thru dates dynamically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885327#M349825</link>
      <description>Thank you I think this will work. I'm new to macro's and I did get the error:&lt;BR /&gt;ERROR: Expected close parenthesis after macro function invocation not found.&lt;BR /&gt;&lt;BR /&gt;I used exactly what you had entered, is it wanting there to be an open/close paranthese after the %dothis?</description>
      <pubDate>Tue, 18 Jul 2023 17:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885327#M349825</guid>
      <dc:creator>hporter</dc:creator>
      <dc:date>2023-07-18T17:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: looping thru dates dynamically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885328#M349826</link>
      <description>Yes the dates in the first table (myTable) are hardcoded and won't change for some time. They look like 01JUN2022, 01JUL2022, 01AUG2022 etc.</description>
      <pubDate>Tue, 18 Jul 2023 17:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885328#M349826</guid>
      <dc:creator>hporter</dc:creator>
      <dc:date>2023-07-18T17:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: looping thru dates dynamically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885329#M349827</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269855"&gt;@hporter&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you I think this will work. I'm new to macro's and I did get the error:&lt;BR /&gt;ERROR: Expected close parenthesis after macro function invocation not found.&lt;BR /&gt;&lt;BR /&gt;I used exactly what you had entered, is it wanting there to be an open/close paranthese after the %dothis?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Surely you can find mis-matched parentheses in a line of code. I don't have to do that for you, do I?&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 17:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885329#M349827</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-18T17:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: looping thru dates dynamically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885332#M349830</link>
      <description>I thought it was an error on the last line invoking the macro, my mistake.</description>
      <pubDate>Tue, 18 Jul 2023 17:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-thru-dates-dynamically/m-p/885332#M349830</guid>
      <dc:creator>hporter</dc:creator>
      <dc:date>2023-07-18T17:59:41Z</dc:date>
    </item>
  </channel>
</rss>

