有人可以告诉我为什么这不解决:
/*put all transaction table names into a data set*/
/*(table names are of format transac_20130603_20130610 (date from and date to)*/
data transaction_tables;
set SASHELP.VTABLE (keep=libname memname);
where lowcase(substr(memname,1,8))='transac_'
run;
/*sort and add rownumbers*/
proc sql;
create table transaction_tables as
select *, monotonic() as rownum
from transaction_tables
order by memname;
run;
/*find rownumber of first and last transaction tables with run dates before campaign start and after end date of campaign*/
data _NULL_;
set transaction_tables;
if substr(memname,9,8)<=&pre_period_start. and substr(memname,18,8)>=&pre_period_start. then do;
call symput("r1", rownum);
stop;
end;
run;
data _NULL_;
set transaction_tables;
if substr(memname,9,8)<=&max_enddate. and substr(memname,18,8)>=&max_enddate. then do;
call symput("r2", rownum);
stop;
end;
run;
%put &r1; %put &r2;
/*r1 = 11, r2 = 27 - both resolving OK*/
/*get all relevant transaction table names where rownumbers are between r1 and r2*/
/*r1=11 and r2=27 so my transaction table name macros should run from t_0 to t_16/*
%macro trans;
%let y = %eval(&r2 - &r1);
%do i=0 %to &y;
data _NULL_;
set transaction_tables;
if rownum = &r2 - (&r2 - &r1 - &i) then do;
call symput("t_&i", cats(libname, '.', memname));
stop;
end;
%end;
%mend trans;
%trans;
%put &t_0;
--WARNING: Macro variable "&t_0" was not resolved
我不完全确定为什么,但是从一些变量中弄乱我认为问题在于它试图将表名分配给t_&amp; i宏的最后一部分。我认为问题在于尝试在尝试调用另一个宏变量时命名一个宏变量(尝试通过在i = 0时调用&amp; i来创建宏t_0)。我想我已经搞砸了语法,因为我认为逻辑相当合理。
谢谢!
答案 0 :(得分:1)
不判断你要做的事情是否有用:
这是一个范围问题。您在宏中创建的任何宏变量仅存在于该宏中。 如果您希望它存在于宏之外,您需要:
在宏中(在第一次提到它之前)将其明确设置为全局,您可以通过编写:
%global t_0;
修改的 另请注意,要结束proc sql,您需要使用quit而不是run。
答案 1 :(得分:1)
替代方法:
proc sql;
create table result as
select cats(libname,'.',memname) as desired
from dictionary.tables
where substr(libname,1,8)='TRANSAC_'
and (scan(libname,2,'_')<=&pre_period_start. and scan(libname,3,'_')>=&pre_period_start.)
and (scan(libname,2,'_')<=&max_enddate. and scan(libname,3,'_')>=&max_enddate.) ;
data _null_;
set result;
call symput(cats('R_',_n_),desired,'g');
run;
甚至可以使用SQL'insert'子句将其重写为一步。 SQL quit;
语句是可选的,我个人从不使用它。