Proc SQL with where statement to select on ID's 的宏
Proc SQL with a macro in where statement to select on ID's
我目前正在尝试编写一个小型 SAS 宏来执行以下操作:
- 宏从输入 table 中读取输入 ID 值:"input_table"。
- ID 值用于在 oracle 数据库中查询变量 "TARGET"。
宏如下图
每当我 运行 宏时,对 ID 的过滤似乎不起作用,并且 proc sql return 为空 table。我无法理解可能出了什么问题,欢迎大家提供帮助!
我当前的解决方案是使用内部联接来完成这项工作。但是,出于效率原因,强烈推荐 SQL 解决方案。
问题:为什么 Proc SQL 不根据列表 "id_list" 选择记录?
%macro query_DB_from_table(input_table = , output_table = );
/* PART 1: Get IDs from the input table */
%local id_list;
proc sql noprint;
select ID into: id_list separated by "' , '"
from &input_table;
quit;
/* PART 2: Query the Oracle Database */
proc sql noprint;
create table &output_table as
select ID, TARGET
from ORACLE_DB
where ID in (%str(')%bquote(&id_list)%str('))
order by ID;
quit;
%mend query_DB_from_table;
QUOTE
函数允许第二个参数指定引号字符。
被引用的值也应该是 TRIM
'd.
更改您的 SQL 以直接将宏变量值填充为完整的逗号分隔的单引号值列表。
示例:
proc sql noprint;
select
quote(trim(name),"'") into :idlist separated by ','
from
sashelp.class
;
quit;
%put &=idlist;
---------- log ----------
IDLIST='Alfred','Alice','Barbara','Carol','Henry','James','Jane',
'Janet','Jeffrey','John','Joyce','Judy','Louise','Mary','Philip','Robert',
'Ronald','Thomas','William'
查询 where 子句会更简单:
…
WHERE ID in ( &id_list )
…
我目前正在尝试编写一个小型 SAS 宏来执行以下操作:
- 宏从输入 table 中读取输入 ID 值:"input_table"。
- ID 值用于在 oracle 数据库中查询变量 "TARGET"。
宏如下图
每当我 运行 宏时,对 ID 的过滤似乎不起作用,并且 proc sql return 为空 table。我无法理解可能出了什么问题,欢迎大家提供帮助!
我当前的解决方案是使用内部联接来完成这项工作。但是,出于效率原因,强烈推荐 SQL 解决方案。
问题:为什么 Proc SQL 不根据列表 "id_list" 选择记录?
%macro query_DB_from_table(input_table = , output_table = );
/* PART 1: Get IDs from the input table */
%local id_list;
proc sql noprint;
select ID into: id_list separated by "' , '"
from &input_table;
quit;
/* PART 2: Query the Oracle Database */
proc sql noprint;
create table &output_table as
select ID, TARGET
from ORACLE_DB
where ID in (%str(')%bquote(&id_list)%str('))
order by ID;
quit;
%mend query_DB_from_table;
QUOTE
函数允许第二个参数指定引号字符。
被引用的值也应该是 TRIM
'd.
更改您的 SQL 以直接将宏变量值填充为完整的逗号分隔的单引号值列表。
示例:
proc sql noprint;
select
quote(trim(name),"'") into :idlist separated by ','
from
sashelp.class
;
quit;
%put &=idlist;
---------- log ----------
IDLIST='Alfred','Alice','Barbara','Carol','Henry','James','Jane',
'Janet','Jeffrey','John','Joyce','Judy','Louise','Mary','Philip','Robert',
'Ronald','Thomas','William'
查询 where 子句会更简单:
…
WHERE ID in ( &id_list )
…