06-18-2015 11:52 AM
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.
|%let date2=%sysfunc(INTNX(month, &Date1, &nextNum));|
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";|
|create table ugd&num as|
|select ERFAC, ORIGBOR, minCIG, FACTYPE, COMMIT|
|proc sort data=ugd# by ORIGBOR ; run;|
|merge ugd&num (in=a)|
|if a and b ;|
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.
06-18-2015 12:34 PM
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*/
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.