1

在 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*/ 

这将提高代码的可读性。

这可能吗?

4

1 回答 1

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);
于 2013-10-17T02:36:53.300 回答