Hi all,
I've had struggled with macro do loops recently, the goal is to extract data from different access tables, one for each month, and put them together using do loops. i was told to use the macro below.
%macro next_month(Month1,nextNum,month2); | |
%let Month1=%sysfunc(substr(&Month1,3,2))%sysfunc(substr(&Month1,6,2)); | |
%let date1=&Month1.01; | |
%let date1=%sysfunc(inputn(&Month1.01,yymmdd.)); | |
%let date2=%sysfunc(INTNX(month, &Date1, &nextNum)); | |
%let &month2=%sysfunc(year(&date2))-%sysfunc(putn(%sysfunc(month(&date2)),Z2.)); | |
%mend; | |
and then combine it with a do loop to finish the task. however, not quite sure how to do it. instead, what i can do is the following.
%macro mth(num) ; | ||
libname aa PCFILES path="C:\test\F_File-2013.accdb"; | ||
proc sql; | ||
create table ugd&num as | ||
select ERFAC, ORIGBOR, minCIG, FACTYPE, COMMIT | ||
from aa.F2013_&num | ||
quit; | ||
proc sort data=ugd# by ORIGBOR ; run; | ||
data result# | ||
merge ugd&num (in=a) | ||
master_airb (in=b); | ||
by ORIGBOR; | ||
if a and b ; | ||
run; | ||
%mend ; | ||
%mth(01); | ||
%mth(02); | ||
%mth(03); |
%mth(04); |
%mth(05); |
%mth(06); |
it does create the results that i wanted, but it doesn't look as nice as using a do loop. can anyone help me make this code more concise ? better be able to use the macro next_month given above.
Thanks!
This paper has information that will help you with this task.
You can eliminate this
proc sort data=ugd# by ORIGBOR ;
by adding an order by ORIGBOR clause to the proc sql that generates ugd&num.
If you provide a few details about the structure of the data set master there may be lots of ways to do this. If the purpose of the merge is to add variables to the data from ugd&num then an sql join is likely in order.
Often when you have a working macro, such as your %mth, then the approach is to call that macro in another that has the loop controls.
Maybe something like:
%macro mthloop(start=, end=);
/* note that for current %do structures start and end should be numeric*/
%do i= &start %to &end;
%let mn=%sysfunc(putn(&i, z2.)); /* puts the leading 0 on the values as needed*/
%mth(&mn);
%end;
%mend;
and call:
%mthloop (start=3,end=6);
Note: since I see things like months and you have what appears to be a FIXED year in your LIBNAME and table name AA.F2013_&num, you may want to consider adding a YEAR parameter.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.