我只想在proc sql中选择变量,只要它们存在

时间:2019-06-10 12:30:55

标签: sas

我想编写一个proc sql查询,该查询将仅选择数据集中存在的那些变量

我在堆栈溢出时找到了答案,并尝试实现它,但对我不起作用。

proc sql noprint;
select cats('a.', name) into :varlist separated by ','
from dictionary.columns
where libname='WORK' and memname='display_type_transposed' and name in 
('_4','_5','_6','_7','_8','_9');
quit;

proc print data=display_type_transposed (obs=100);
run;

%put &=varlist.;

proc sql;
create table merged_display_flags as
select a.*, &varlist.
from display_loc_transposed as a, display_type_transposed as b
where a.period_id=b.period_id and a.store_id=b.store_id and 
a.PRODUCT_ID=b.PRODUCT_ID;
quit;

我收到以下错误消息。

WARNING: Apparent symbolic reference VARLIST not resolved.
ERROR 22-322: Syntax error, expecting one of the following: a name, a 
quoted string, a numeric constant, a datetime constant,
a missing value, BTRIM, INPUT, PUT, SUBSTRING, USER.

409        from display_loc_transposed as a, display_type_transposed as b
            ______________________
            22
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, 
**, +, ',', -, /, <, <=, <>, =, >, >=, ?, AND, AS,
CONTAINS, EQ, EQT, FROM, GE, GET, GT, GTT, LE, LET, LIKE, LT, LTT, NE, 
NET, OR, ^=, |, ||, ~=.

410        where a.period_id=b.period_id and a.store_id=b.store_id and 
a.PRODUCT_ID=b.PRODUCT_ID;

1 个答案:

答案 0 :(得分:1)

元数据视图/表DICTIONARY.COLUMNS中的MEMNAME值始终为大写。因此,您的查询未找到具有这些名称的任何变量,因为您以小写字母搜索成员名称。因此它永远不会使宏变量。

防止未找到任何匹配观测值时未定义的宏变量错误的一种简单方法是在查询之前将目标宏变量设置为空。

%let varlist=;

但是当VARLIST为空时,您的SQL代码将不起作用,因为这样您的查询中将带有一个逗号。

您可以测试自动宏变量SQLOBS以查看是否生成了任何变量。

%if (&SQLOBS>0) %then %do;
...
%end;