【发布时间】:2016-04-04 20:49:55
【问题描述】:
希望能解释清楚。我正在使用 SAS 8.2(这是我公司使用的)并尝试获取在 create_var 宏中创建的名为 &op_var_name 的变量名称。每次 run_loops 宏运行时都会改变。例如,它将基于 ds_split_list 数据集创建 list_detail1、list_detail2 和 list_detail3。我需要稍后在代码中使用这些变量,我似乎无法让它工作。我尝试在 run_loops 和 create_var 宏中添加 %global &op_var_name。日志在 run_loop 部分显示带有数据的变量,但随后在代码中引用该变量时日志显示为空白。
%macro create_var(ds_in=, item_to_list=, op_var_name=);
data &op_var_name;
set &ds_in end=eof;
length gen_list $30000.;
retain gen_list;
if not eof then do;
gen_list=trim(left(gen_list))||"'"||trim(left(&item_to_list))||"'"|| ",";
end;
if eof then do;
gen_list=trim(left(gen_list))||"'"||trim(left(&item_to_list))||"'";
call symput("gen_name",trim(left(gen_list)));
output;
end;
run;
%put &op_var_name=&gen_name ;
%mend create_var;
data _null_;
set ds_split_list;
call symput ('nobs', _n_);
call symput ('ds_feed'||left(_n_),memname);
call symput ('item_to_list', 'lotlabel');
call symput ('op_var_name'||left(_n_), 'list_detail'||left(_n_));
run;
%macro run_loops;
/* &nobs and all the "&&var&i" variables defined above */
%do i = 1 %to &nobs;
%create_var(ds_in=&&ds_feed&i, item_to_list=&&item_to_list, op_var_name=&&op_var_name&i);
%end;
%mend run_loops;
%run_loops;
/* Later in the code is where I reference the variable created above. */
%get_oper(list=&list_detail1);
谢谢!
编辑:我最终使用了 Tom 回复中的这个选项。
或者您可以在子宏中创建变量时将其设为全局变量。
%global &op_var_name;
%let &op_var_name=&gen_name ;
【问题讨论】:
标签: variables macros nested sas