DATA Step, Macro, Functions and more

question about using sas macro do loops

Occasional Contributor
Posts: 8

question about using sas macro do loops

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.));                                                  

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                                                                                                                                                              
proc sort data=ugd# by ORIGBOR ; run;                                                                                                                                         
data result#                                                                                                                                                                  
merge ugd&num (in=a)                                                                                                                                                              
  master_airb (in=b);                                                                                                                                                         
by ORIGBOR;                                                                                                                                                                       
if a and b ;                                                                                                                                                                      
%mend ;                                                                                                                                                                           

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.


Regular Contributor
Posts: 227

Re: question about using sas macro do loops

This paper has information that will help you with this task.

Super User
Posts: 13,583

Re: question about using sas macro do loops

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*/




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.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation