【问题标题】:SAS: use dynamic macro variable in loop in macro programSAS:在宏程序的循环中使用动态宏变量
【发布时间】:2018-11-04 21:51:18
【问题描述】:

我想达到的一样:

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

但我不知道如何动态更改“变量”var。

谢谢!


类似的问题:

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

【问题讨论】:

  • 您的宏并没有完全尝试重新创建数据集,而是将每个变量提取到单独的数据集中。如果您尝试创建事实/暗淡表,则 SAS 社区上有一个宏可以自动执行此操作。

标签: loops sas sas-macro


【解决方案1】:

你很亲密。下面的东西应该适合你。 call 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多