Solved

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

Posted on 2010-09-16
9
561 Views
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.

rsubmit;
%macro t1(table,step,start,end);
proc sql;
create table &table as
select *
from Month_Jun10
where &step between &start and &end;
quit;
%mend;
%t1(step22,step22_date,'01JUL2010'D,'31JUL2010'D);
%t1(step10,step10_date,'01JUL2010'D,'31JUL2010'D);
%t1(step16,step16_date,'01JUL2010'D,'31JUL2010'D);
%t1(step16,step16_date,'01JUL2010'D,'31JUL2010'D);
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 .;
quit;
data Total;
set step22 step10 step16 step16 REM;run;
endrsubmit;

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

rsubmit;
%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;
quit;
%mend;
%mth1(OCT09,'01OCT2009'D,'31OCT2009'D);
%mth1(NOV09,'01NOV2009'D,'30NOV2009'D);
%mth1(DEC09,'01DEC2009'D,'31DEC2009'D);
%mth1(JAN10,'01JAN2010'D,'31JAN2010'D);
%mth1(FEB10,'01FEB2010'D,'28FEB2010'D);
%mth1(MAR10,'01MAR2010'D,'31MAR2010'D);
%mth1(APR10,'01APR2010'D,'30APR2010'D);
%mth1(MAY10,'01MAY2010'D,'31MAY2010'D);
%mth1(JUN10,'01JUN2010'D,'30JUN2010'D);
%mth1(JUL10,'01JUL2010'D,'30JUL2010'D);
endrsubmit;

Thanks in advance,
SASnewbie
0
Comment
Question by:SASnewbie
  • 6
9 Comments
 

Author Comment

by:SASnewbie
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:

'01OCT2009'D,'31OCT2009'
'01NOV2009'D,'30NOV2009'
'01DEC2009'D,'31DEC2009'
'01JAN2010'D,'31JAN2010'
'01FEB2010'D,'28FEB2010'
'01MAR2010'D,'31MAR2010'
'01APR2010'D,'30APR2010'
'01MAY2010'D,'31MAY2010'
'01JUN2010'D,'30JUN2010'
'01JUL2010'D,'30JUL2010'
'01AUG2010'D,'31AUG2010'
 Then the same process is repeated for Month_Nov09, through Month_Aug10.
Thanks again,
SASnewbie
0
 

Author Comment

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

Doc3.doc
0
 

Author Comment

by:SASnewbie
ID: 33692981
The "Total" value for each iteration is the incremental value for that "Month_XXX".
Thanks again,
 
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:SASnewbie
ID: 33693134
Please change this question to the Database -> SAS zone.
Thanks,
0
 
LVL 11

Accepted Solution

by:
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 ******************** ;

%MEND;

%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()) );
   %xxx(&datex);
   %let datex = %SYSFUNC(INTNX(MONTH,&datex,1,e)); /* Increment the datex variable by 1 month */
%END;
%MEND;

%Repx;
0
 

Author Comment

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

Author Comment

by:SASnewbie
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 )
********************
Fantastic!!!!
Thanks,
 SASnewbie
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle query help 18 99
understanding output of mysql version 2 50
Search combo error "Data Type Mismatch in Criteria Expression" 2 51
ORA-12560: TNS:protocol adapter error 8 83
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

912 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

23 Experts available now in Live!

Get 1:1 Help Now