What method can be used to run a segment of code iteratively with different date values?

Posted on 2010-09-16
Last Modified: 2013-11-16
I have the following segment code below that I have to change the dates to get a full dataset for the range of dates. For this one the dates will be from October 2009 to August 2010.

%macro t1(table,step,start,end);
proc sql;
create table &table as
select *
from Month_Jun10
where &step between &start and &end;
proc sql;
create table REM as
select *
from Month_Jun10
where Add_date between 20100701 and 20100731
and active_flg eq 'NotActiveLM'
and step22_date eq .
and step10_date eq .
and step16_date eq .
and step16_date eq .;
data Total;
set step22 step10 step16 step16 REM;run;

The creation of the "month" tables requires the same changes.

%macro mth1(pd,start,end);
proc sql;
create table Month_&pd as
select *
from dima.hamp_daily_&gdate
where step45_date between &start and &end;

Thanks in advance,
Question by:SASnewbie
  • 6

Author Comment

ID: 33692474
I need to add more explanation to what changes.
for each month; oct09 through aug10, the start and end dates have to run through 01OCT2009 through 31AUG2010,
So Month_Oct09 has to run it's segment 11 times for the following start and end dates:

 Then the same process is repeated for Month_Nov09, through Month_Aug10.
Thanks again,

Author Comment

ID: 33692834
Attached is where the captured output looks like:


Author Comment

ID: 33692981
The "Total" value for each iteration is the incremental value for that "Month_XXX".
Thanks again,
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Author Comment

ID: 33693134
Please change this question to the Database -> SAS zone.
LVL 11

Accepted Solution

theartfuldazzler earned 500 total points
ID: 33699254
Hi SASNewbie

I do a lot of repeated code "per month", and make use of the INTNX function. (see SAS help for details.

The general code below works well - you look like you understand macros, so you would need to fill in your steps that are particular to you.  Hope this helps:

%MACRO XXX (date);

%Let mon = %SYSFUNC(PUTN(&date,MONYY5.));  /*  Create variable to look like MMMYY */
%Put mon = &mon;

%Let Start = %SYSFUNC(INTNX(MONTH,&date,0,b));     /* Create Date at start of month */
%PUT Start = &Start (%SYSFUNC(PUTN(&start,date9.)) );

%Let End = %SYSFUNC(INTNX(MONTH,&date,0,e));   /* Create date at end of month */
%PUT End = &End (%SYSFUNC(PUTN(&end,date9.)) );
%PUT ******************** ;


%Macro Repx;

%Let Datex = %SYSEVALF("31Jan2009"d);  /* hardcode the date */

%let Datex = %SYSFUNC(INTNX(Month, %SYSFUNC(TODAY()), -12, b));  /* Start from 12 months before today */

%DO %UNTIL (&datex >= %SYSFUNC(TODAY()) );
   %let datex = %SYSFUNC(INTNX(MONTH,&datex,1,e)); /* Increment the datex variable by 1 month */


Author Comment

ID: 33700422
Hi theartfuldazzler,
Thank you for responding.
Your code looks really good. I will work on it shortly and get back to you.

Author Comment

ID: 33701300
Hi again theartfuldazzler,
This is the output from your code and is exactly what I am looking for:
mon = SEP09
Start = 18141 (01SEP2009 )
End = 18170 (30SEP2009 )
mon = OCT09
Start = 18171 (01OCT2009 )
End = 18201 (31OCT2009 )
mon = NOV09
Start = 18202 (01NOV2009 )
End = 18231 (30NOV2009 )
mon = DEC09
Start = 18232 (01DEC2009 )
End = 18262 (31DEC2009 )
mon = JAN10
Start = 18263 (01JAN2010 )
End = 18293 (31JAN2010 )
mon = FEB10
Start = 18294 (01FEB2010 )
End = 18321 (28FEB2010 )
mon = MAR10
Start = 18322 (01MAR2010 )
End = 18352 (31MAR2010 )
mon = APR10
Start = 18353 (01APR2010 )
End = 18382 (30APR2010 )
mon = MAY10
Start = 18383 (01MAY2010 )
End = 18413 (31MAY2010 )
mon = JUN10
Start = 18414 (01JUN2010 )
End = 18443 (30JUN2010 )

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now