SAS Error: Subquery evaluated to more than one row

SAS Error: Subquery evaluated to more than one row

我收到消息“错误:子查询评估为不止一行。我已经在下面发布了工作代码。我想知道如何解决这个错误。提前谢谢你。

data have;
input Subject Type :. Date &:anydtdte. Procedure :. Measurement;
format date yymmdd10.;
datalines;

500   Initial    15 AUG 2017      Invasive     20 
500   Initial    18 SEPT 2018     Surface      35 
500   Followup   12 SEPT 2018     Invasive     54 
428   Followup    2 JUL 2019      Outer        29 
765   Seventh     3 JUL 2018      Other        13 
500   Followup    6 NOV 2018      Surface      98 
428   Initial     23 FEB 2018     Outer        10 
765   Initial     20 AUG 2019     Other        19 
610   Third       21 AUG 2018     Invasive     66 
610   Initial     27 Mar 2018     Invasive     17 
999   Dummy       17 mar 2020     Some          1
999   Dummy       18 mar 2020     Some          2
999   Dummy       19 mar 2020     Some          3
;

proc sql;
create table want as
select *,
    (select max(measurement) 
     from have 
     where subject=a.subject and type=a.type and procedure=a.procedure 
     having date = max(date)) / min(measurement) as ratio
from have as a
group by subject, type, procedure
order by subject, date;
quit;

当您 运行 查询您在现实生活中使用的 完整数据集 时收到消息 "ERROR: Subquery evaluated to more than one row" 的原因是因为对于 至少一种分组变量 的组合,您有 max(date) 值重复(即根据上述评论,我们假设中的 subjecttype) .

事实上,您在子查询 having date = max(date) 中应用的条件将 return 与每个分组变量的 date 等于 max(date) 的记录一样多的行组合。

我的建议是确保输入数据集每个 subjecttypedate.(*)

只有一条记录

如果这样做,您的查询将正常工作(尽管您可以在子查询中将 max(measurement) 替换为简单的 measurement,因为每个组只有一个记录具有 date = max(date)).

因此,最终查询将是:

PROC SQL;
    create table want as
    select a.*,
        (select measurement as measurement_last_date
         from have
         where subject = a.subject and type = a.type 
         having date = max(date)) / min(a.measurement) as ratio
    from have as a
    group by subject, type
    order by subject, type, date;
QUIT;

如果您 运行 输入数据集中的此代码(我已将记录 12 的 measurement 的值从 2 更改为 0.2 以使结果更加清晰),您将得到:

Obs    Subject    Type              Date    Procedure    Measurement     ratio
 1      428      Followup    2019-07-02    Outer            29.0        1.0000
 2      428      Initial     2018-02-23    Outer            10.0        1.0000
 3      500      Followup    2018-09-12    Invasive         54.0        1.8148
 4      500      Followup    2018-11-06    Surface          98.0        1.8148
 5      500      Initial     2017-08-15    Invasive         20.0        1.7500
 6      500      Initial     2018-09-18    Surface          35.0        1.7500
 7      610      Initial     2018-03-27    Invasive         17.0        1.0000
 8      610      Third       2018-08-21    Invasive         66.0        1.0000
 9      765      Initial     2019-08-20    Other            19.0        1.0000
10      765      Seventh     2018-07-03    Other            13.0        1.0000
11      999      Dummy       2020-03-17    Some              1.0       15.0000
12      999      Dummy       2020-03-18    Some              0.2       15.0000
13      999      Dummy       2020-03-19    Some              3.0       15.0000

(*) 此查询无误运行的实际条件是每个 subjecttype"max(date) over subject-type"[=48 有一条记录=],尽管这种情况(在 max(date) 上保证唯一性,而不是在每个组 每个 date 上保证唯一性)在实际业务案例中不太可能发生.