运行 一次使用不同参数多次执行一个宏

run a macro multiple times with different parameters at once

所以我有这个宏:stab_index(yearmonth,period)

假设我必须 运行 5 次(可能更多)使用不同的参数,例如

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

为了给 运行 另一个宏生成足够的数据集:Stab_Ind_DYNAMICS

但我不想运行 6 次才能得到结果,我想运行 一次全部完成,而不必每次都填写参数。

有人可以告诉我如何设置吗?

谢谢!

您可以使用另一个循环遍历参数列表的宏来实现此目的。

%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(&param1,%str( )));
        %let thisparam1=%scan(&param1,&i,%str( ));
        %let thisparam2=%scan(&param2,&i,%str( ));
        %put &thisparam1 &thisparam2;
        %stab_index(&thisparam1,&thisparam2);
    %end;
%mend loop;
%loop;

您首先需要定义参数列表(我在这里将它们称为 param1 和 param2)。

然后您可以从 1 循环到单词数并从列表中检索第 i 个参数并在您的 stab_index 宏中使用它。

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(&param1,'-'))) and a third parameter in the scan function (%scan(&param1,&i,'-')).

这假设您的参数值始终存在于您的数据中。如果您可以将数据集缩小到 yearmonthperiod 的每个唯一组合(我的 unique 数据集如下所示),那么您不需要输入任何内容,只需让数据做可以适应不断变化的数据的工作:

** 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 &macro_list.;