是否可以将宏传递给宏?

时间:2013-10-17 01:46:35

标签: macros sas code-readability

在SAS中,我可以使用这个方便的代码片段来做这样的事情。

%let listofvars = work.apples work.bananas work.oranges; 
%let var_no = 1;
%let var = %scan(&listofvars, &var_no, ' ');
%do %while (&var ne);
    proc sort data = &var; 
        by id;
    run; 

    %let var_no = %eval(&var_no +1);
    %let var = %scan(&listofvars, &var_no, ' ');
%end;

对每个数据集进行排序。

我非常希望将代码段缩减为循环宏,所以我可以这样做:

%let setlist = work.apples work.bananas work.oranges;

%macro mymacro(dataset);
    proc sort data = &dataset.
    by id;
    run;
%mend;

%loop(&setlist, mymacro); 
/*the loop macro will know to pass the &var. in as a arguement to the macro*/ 

这将提高代码可读性。

这可能吗?

1 个答案:

答案 0 :(得分:2)

是。名称宏例程可以是宏。宏为您“编写”SAS代码。

%macro create(dataset);
data &dataset;
do i=1 to 10;
    id=rannor(0);
    output;
end;
run;
%mend;

%macro sort(dataset);
proc sort data=&dataset;
by id;
run;
%mend;

%macro loop(list,mcr);
%local i n val ;
%let n=%sysfunc(countw(&list));
%do i=1 %to &n;
    %let val = %scan(&list,&i);
    %&mcr(&val);
%end;
%mend;  

%let sets = apples oranges pears;
options mprint;
%loop(&sets,create);
%loop(&sets,sort);