Hello
I have the following challenge.
There is a SAS program that depends on one user macro parameter (month).
The user need to define month in %let statement.
For example %let month=1904; (It means April 2019)
IF we are in April 2019 then user need to run 4 times (Because from January til April there are 4 runs need to be done)
%let month=1901;
%let month=1902;
%let month=1903;
%let month=1904;
I know the way to change the program to macro.
%macro RRR(month);
....................................
code
%mend RRR;
RRR(1904);
RRR(1903);
RRR(1902);
RRR(1901);
My question:
How can I run the macros RRR automatically from January this year till current month?
For example:
IF user run it in December 2019 then 12 macros will run automatically
IF user run it in Novemeber 2019 then 11 macros will run automatically
IF user run it in April 2019 then 4 macros will run automatically
The only argument that user define is current month (in format YYMM for instance 1912)
Use a wrapper macro:
%macro rrr(month);
%put &=month.;
%mend;
%macro run_all;
%local mm mo;
%do mm = 1 %to %sysfunc(month(%sysfunc(today())));
%let mo=19%sysfunc(putn(&mm.,z2.));
%rrr(&mo.)
%end;
%mend;
%run_all
or call execute from a data step:
data _null_;
do mm = 1 to month(today());
call execute('%nrstr(%rrr(19'!!put(mm,z2.)!!'))');
end;
run;
data have;
input Month;
cards;
1904
1905
1906
;
run;
data _null_;
set have;
rc=dosubl(cats('%RRR(',month,')'));
run;
Let me know!
Best,
Use a wrapper macro:
%macro rrr(month);
%put &=month.;
%mend;
%macro run_all;
%local mm mo;
%do mm = 1 %to %sysfunc(month(%sysfunc(today())));
%let mo=19%sysfunc(putn(&mm.,z2.));
%rrr(&mo.)
%end;
%mend;
%run_all
or call execute from a data step:
data _null_;
do mm = 1 to month(today());
call execute('%nrstr(%rrr(19'!!put(mm,z2.)!!'))');
end;
run;
You can do the loop either by a data step or by a macro:
1) using data step:
%let month = 201904;
data _null:
length month $6;
month = symget("month");
startm = input(substr(month,1 4)!!'01' , 6.);
endm = input(month,6.);
do mm from startm to endm;
call execute("%rrr(" !! put(mm,6.) !! ");" );
end;
run;
2) using macro program:
%let month = 201904;
%macro loop;
%let yy = %substr(&month,1,4);
%let mm1 =01;
%let mme = %substr(&month,5,2);
%do i=&mm1 %to &mme;
%if %length(&i) = 1 %then %let i = 0&i;
%rrr(&yy&i);
%end;
%mend;
%loop;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.