BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14


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)

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
ed_sas_member
Meteorite | Level 14
Hi @Ronein ,
 
I believe you can create data-driven macro calls using the DOSUBL function as follows. 
To use it, you need to have a dataset containing the different months values in the variable month.
 
data have;
	input Month;
	cards;
1904
1905
1906
;
run;
data _null_;
set have;
rc=dosubl(cats('%RRR(',month,')'));
run;

Let me know!

 

Best,

Kurt_Bremser
Super User

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;
Shmuel
Garnet | Level 18

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 945 views
  • 3 likes
  • 4 in conversation