SAS:在宏程序中循环使用动态宏变量

时间:2018-11-04 21:51:19

标签: loops sas sas-macro

我想要达到以下目标:

data train_Sex(keep=Name Sex) train_Age(keep=Name Age) train_Height(keep=Name Height) train_Weight(keep=Name Height);
    set sashelp.class;
run;

使用带有变量列表的宏程序。 据我所知:

* Build macro program;
%macro build_sets(var_list);
    %let nwords = %sysfunc(countw(&var_list));

    %do i=1 %to &nwords;
        call symput("variable",  %scan(&var_list, i));

        data train_&variable(keep=Name &variable);
            set sashelp.class;
        run;

    %end;
%mend;

* Run it;
%let var_list = Sex Age Height Weight;
%build_sets(&var_list);

但是我缺乏如何动态更改“变量”变量的知识。

谢谢!


类似的问题:

1。SAS dynamically declaring macro variable 2. Using a dynamic macro variable in a call symput statement 3. Dynamic macro variable access SAS

1 个答案:

答案 0 :(得分:1)

你很近。下面的东西应该为您工作。调用symput是datastep的一部分,用于从dvariables创建宏变量,因此就产生了问题。

 %macro build_sets(var_list);
;

%do i=1 %to  %sysfunc(countw(&var_list));
    %let variable=  %scan(&var_list, &i));

    data train_&variable(keep=Name &variable);
        set sashelp.class;
    run;

%end;
%mend;

 * Run it;
%let var_list = Sex Age Height Weight;
%build_sets(&var_list);