SAS 宏变量无法解析
SAS macro variable do cannot resolve
这是一个相当愚蠢的例子,但它保留了我正在尝试做的事情的本质(使用 SAS 大学版):
data TableList;
input tables $ cols $;
cards;
tab1 col
tab2 cul
;
run;
%macro test;
proc sql;
select tables
into:tabs separated by " "
from TableList;
quit;
%do i=1 %to 2;
%let t = %scan(&tabs,&i);
proc sql;
select cols
into: col
from TableList
where tables='&t';
quit;
%put &col;
%end;
%mend;
%test;
这个问题是当我 运行 这个代码时我收到这个错误消息:
WARNING: Apparent symbolic reference COL not resolved.
&col
这是为什么。 sas 不会在 运行 时用它的真实值改变 &col 吗?
更新:
设置“&t”而不是“&t”解决了我的问题。代码现在可以运行了。
data TableList;
input tables $ cols $;
cards;
tab1 col
tab2 cul
;
run;
%macro test;
proc sql;
select tables
into:tabs separated by " "
from TableList;
quit;
%do i=1 %to 2;
%let t = %scan(&tabs,&i);
proc sql;
select cols
into: col
from TableList
where tables="&t";
quit;
%put Column &col;
%end;
%mend;
%test;
这里有几个问题
where tables='&t'
将因为单引号而不起作用。使用宏变量时必须使用双引号。
另外&t
未定义
这似乎可行(即 在日志中打印 cul
),但我必须手动定义 t
。
data TableList;
input tables $ cols $;
cards;
tab1 col
tab2 cul
;
run;
%let t=tab2;
%macro test;
proc sql;
select tables
into:tabs separated by " "
from TableList;
quit;
%do i=1 %to 2;
%let t = %scan(&tabs,&i);
proc sql;
select cols
into: col
from TableList
where tables="&t";
quit;
%put &col;
%end;
%mend;
%test;
这是一个相当愚蠢的例子,但它保留了我正在尝试做的事情的本质(使用 SAS 大学版):
data TableList;
input tables $ cols $;
cards;
tab1 col
tab2 cul
;
run;
%macro test;
proc sql;
select tables
into:tabs separated by " "
from TableList;
quit;
%do i=1 %to 2;
%let t = %scan(&tabs,&i);
proc sql;
select cols
into: col
from TableList
where tables='&t';
quit;
%put &col;
%end;
%mend;
%test;
这个问题是当我 运行 这个代码时我收到这个错误消息:
WARNING: Apparent symbolic reference COL not resolved.
&col
这是为什么。 sas 不会在 运行 时用它的真实值改变 &col 吗?
更新: 设置“&t”而不是“&t”解决了我的问题。代码现在可以运行了。
data TableList;
input tables $ cols $;
cards;
tab1 col
tab2 cul
;
run;
%macro test;
proc sql;
select tables
into:tabs separated by " "
from TableList;
quit;
%do i=1 %to 2;
%let t = %scan(&tabs,&i);
proc sql;
select cols
into: col
from TableList
where tables="&t";
quit;
%put Column &col;
%end;
%mend;
%test;
这里有几个问题
where tables='&t'
将因为单引号而不起作用。使用宏变量时必须使用双引号。
另外&t
未定义
这似乎可行(即 在日志中打印 cul
),但我必须手动定义 t
。
data TableList;
input tables $ cols $;
cards;
tab1 col
tab2 cul
;
run;
%let t=tab2;
%macro test;
proc sql;
select tables
into:tabs separated by " "
from TableList;
quit;
%do i=1 %to 2;
%let t = %scan(&tabs,&i);
proc sql;
select cols
into: col
from TableList
where tables="&t";
quit;
%put &col;
%end;
%mend;
%test;