sas宏执行循环和&符号到子集数据集

时间:2016-02-09 23:23:28

标签: list loops macros sas

在下面的宏中,如何在数据步骤中自动从sashelp.cars中选择汽车。我认为它涉及到&符号            %let dsn =& inDsn; ....

       %macro subset_by_make (dsn=,carList=);

    %let car_n = %sysfunc(countw(&carList, ' '));

    %do i = 1 %to &car_n;
    %let make = %scan (&carList, &i, ' ');
    data cars_&make;
        set sashelp.cars(where = (make = "&make"));
    run;
    proc print data=cars_&make;
    run;
%end;
 %mend subset_by_make;
 %subset_by_make(dsn=sashelp.cars, carList= Acura Toyota);

非常感谢任何帮助 玛吉

2 个答案:

答案 0 :(得分:1)

如果您只想打印出按make分组的汽车数据集,则根本不需要任何宏代码:

proc sort data = sashelp.cars out = cars;
by make;
run;

proc print data = cars;
where make in ('Acura','Toyota');
by make;
run;

答案 1 :(得分:0)

Reeza,对不起,我的问题令人困惑。我想从sashelp.cars创建名为cars_acura,cars_toyota等的各种数据集

下面,我想我找到了一个解决方案。谢谢大家的帮助!

%macro subset_by_make (lib=,inDsn=,carList=);

    %let car_n = %sysfunc(countw(&carList, ' '));

    %do i = 1 %to &car_n %by 1;
        %let make = %scan (&carList, &i, ' ');      
      title "dataset: &inDsn._&make";
        data &inDsn._&make;
            set &lib..&inDsn(where = (make = "&make"));
        run;
        proc print data=cars_&make;
        run;
    %end;
%mend subset_by_make;
%subset_by_make(lib=sashelp,inDsn=cars,carList= Acura Toyota);