SAS 过程 sql 错误。在贡献表中找不到以下列:

SAS proc sql error. The following columns were not found in the contributing tables:

我是 运行 SAS 中的以下过程 sql 代码。我得到的错误是“在贡献表中找不到以下列:我真的不明白错误在哪里。

proc sql;
 create table sum_Med
  as
  select ID, count(prop_ID) as count,
  (case 
    when (count >= 5) then 1 else 0 end) as poli
  from dist_Id
 group by ID;
quit;

错误如下:

Error The following columns were not found in the contributing tables: count

一般来说,SQL 不允许您在定义它们的同一 SELECT 中引用列别名。这就是您收到错误的原因。

但是,proc sql 有一个方便的解决方法——calculated 关键字。所以你可以这样写:

create table sum_Med as
  select ID, count(prop_ID) as count,
         (case when calculated count >= 5 then 1 else 0 end) as poli
  from dist_Id
  group by ID;

当然,你也可以只重复表达:

create table sum_Med as
  select ID, count(prop_ID) as count,
         (case when count(prop_ID) >= 5 then 1 else 0 end) as poli
  from dist_Id
  group by ID;

这是解决问题的传统方法 -- 如果您在与另一个数据库的连接中使用本机 SQL,则可能需要这种方法。