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.
... View more