![](/img/trans.png)
[英]Using a SAS Macro Variable and link it to the Contain Operator in Proc SQL
[英]Using a SAS macro variable to select all values using the IN operator in PROC SQL
在SAS腳本中,我有一個宏變量,該宏變量隨后在PROC SQL步驟的SQL in
語句中使用。
%let my_list = (1,2,3);
proc sql;
select *
from my_table
where var1 in &my_list.
;
quit;
這可以正常工作,但是我需要一些靈活性,並且還希望能夠選擇所有行而不更改SQL代碼本身,而只是更改宏變量。
有沒有技巧指定宏變量,以便它仍使用IN運算符選擇所有行? (避免使用子查詢解決方案來填充宏變量中的所有可能值)
您可以將代碼更改為
%let where_clause = var1 in (1,2,3);
proc sql;
select *
from my_table
where &where_clause
;
quit;
並將宏變量更改為%let where_clause = 1=1;
為了選擇所有行。
%let where_clause = 1=1;
proc sql;
select *
from my_table
where &where_clause
;
quit;
或者 ,如果您堅持要保持代碼不變,則可以按如下所示更改宏變量,以使where子句始終為true:
%let my_list = (1) or 1=1;
proc sql;
select *
from my_table
where var1 in &my_list
;
quit;
(骯臟但完成工作)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.