简体   繁体   English

Proc Sql中的SAS子查询

[英]SAS Subquery in Proc Sql

I have a table containing a list of libnames and their tables. 我有一个表,其中包含libname及其表的列表。 I'd like to create a subquery to return the minimum createdon date for each listed table, passing the libname and table as the libname.table used in the subquery. 我想创建一个子查询以返回列出的每个表的最短日期,并将libname和表作为子查询中使用的libname.table传递。

I've created the following: 我创建了以下内容:

proc sql;
create table createdon_min as
select *,
cats("(select min(createdon) from "||strip(libname)||"."||strip(table_name)||")") as CREATEDON_FRM
from all_tables1
where freq="DAILY" AND part_date="CREATEDON";
quit;

No error is generated, but the createdon_frm value doesn't execute, it merely creates a string value like "(select min(createdon) from pvaprov.institutional)". 不会产生错误,但是createdon_frm值不会执行,它只会创建一个字符串值,例如“(从pvaprov.institutionalal中选择min(createdon))”。

The libname and table resolved correctly, but how do I get the statement to execute as a subquery? libname和表已正确解析,但是如何使该语句作为子查询执行?

You cannot dynamically call sql queries from another sql query. 您不能从另一个SQL查询动态调用SQL查询。 This solutions uses the datastep and call execute to do the proc sql and append the result to a base dataset. 此解决方案使用datastep并调用execute来执行proc sql并将结果附加到基本数据集。

data want;
 length mincreatedon 8.;
 stop;
run;

data _null_;
 set all_tables1;
 where freq="DAILY" AND part_date="CREATEDON";
 call execute("proc sql;create table a as select min(createdon) as mincreatedon from "||strip(libname)||"."||strip(table_name)||";quit;proc append base=want data=a;run;");
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM