Reputation: 103
So I have this macro: stab_index(yearmonth,period)
.
Let's say that I have to run it 5 times (maybe more) with different parameters like this
%stab_index(201601,01/2016);
%stab_index(201602,02/2016);
%stab_index(201603,03/2016);
%stab_index(201604,04/2016);
%stab_index(201605,05/2016);
in order to generate a adequate dataset to run another macro: Stab_Ind_DYNAMICS
.
But I don't want to run 6 times to get the result, I would like to run all of them at once without having to fill the parameters every time.
Can someone point me in the direction of how I would set this up?
Thanks!
Upvotes: 0
Views: 3069
Reputation: 4006
You can achieve this with another macro which loops trough your list of parameters.
%let param1 = 201601 201602 201603 201604 201605;
%let param2 = 01/2016 02/2016 03/2016 04/2016 05/2016;
%macro loop();
%do i=1 %to %sysfunc(countw(¶m1,%str( )));
%let thisparam1=%scan(¶m1,&i,%str( ));
%let thisparam2=%scan(¶m2,&i,%str( ));
%put &thisparam1 &thisparam2;
%stab_index(&thisparam1,&thisparam2);
%end;
%mend loop;
%loop;
You first need to define your lists of parameters (I called them param1 & param2 here).
Then you can loop from 1 to the number of words and retrieve the i'th paramter from the list and use it in your stab_index macro.
Just in case you parameters contains spaces, you can use another separator than spaces for your lists and define it with a 2nd argument in the countw function (
%sysfunc(countw(¶m1,'-'))
) and a third parameter in the scan function (%scan(¶m1,&i,'-')
).
Upvotes: 0
Reputation: 873
This assumes your parameter values always exist within your data. If you can get your dataset down to every unique combination of yearmonth
and period
(how my unique
dataset looks below), then you don't need to input anything, just let the data do the work which can accommodate changing data:
** create test data **;
data have0;
year = 2016;
do i=1 to 12;
temp=i;
output;
end;
run;
data have; set have0;
temp1 = strip(put(year,best4.))||strip(put(temp,z2.));
yearmonth=intnx('month', input(put(temp1,6.), yymmn6.), 1)-1;
period=yearmonth;
format yearmonth yymmn6. period mmyys7.;
run;
** get data down to every unique combination of yearmonth and period **;
proc sort data = have out=unique(keep=yearmonth period) nodupkey;
by yearmonth period;
run;
** create a macro string dynamically using data **;
data create_macro_string; set unique;
macro_str=%nrstr("%stab_index")||"("||strip(put(yearmonth,yymmn6.))||","||strip(put(period,mmyys7.))||");";
keep yearmonth period macro_str;
run;
** put all your macros into a list **;
proc sql noprint;
select macro_str
into: macro_list separated by " "
from create_macro_string;
quit;
** call your macros **;
%put ¯o_list.;
Upvotes: 1