简体   繁体   English

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

[英]sas macro do loop and ampersand to subset dataset

In the macro below, how do I automate selecting cars from sashelp.cars in my data step. 在下面的宏中,如何在数据步骤中自动从sashelp.cars中选择汽车。 I think it involves ampersands %let dsn = &inDsn; 我认为这涉及到&号%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);

Would appreciate any help Maggie 希望对Maggie有所帮助

There's no need for any macro code at all here if all you want to do is print out the cars dataset grouped by make: 如果您只想打印出按make分组的汽车数据集,则这里根本不需要任何宏代码:

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

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

Reeza, sorry my question is confusing. 丽莎,对不起,我的问题令人困惑。 I wanted to create various datasets called cars_acura, cars_toyota, etc. from sashelp.cars 我想从sashelp.cars创建各种名为cars_acura,cars_toyota等的数据集

Below, I think I have found a solution. 下面,我想我已经找到了解决方案。 Thanks everyone for your help! 谢谢大家的帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM