1

我有以下数据:

acct        date
11111       01/01/2014
11111       01/01/2014
11111       02/02/2014
22222       01/01/2014
22222       01/01/2014
33333       01/01/2013
33333       03/03/2014
44444       01/01/2014
44444       01/01/2014
44444       01/01/2014

在 SAS 中完成以下任务的最佳方法是什么?我想比较每个帐户编号的日期并返回帐户的所有记录,其中至少有一个日期不匹配。

所以对于上面的数据集,我想得到以下结果:

acct        date
11111       01/01/2014
11111       01/01/2014
11111       02/02/2014
33333       01/01/2013
33333       03/03/2014
4

2 回答 2

2

一个 PROC SQL 就可以解决问题。使用 count(distinct date) 计算不同日期的数量。按 acct 分组以按 acct 进行计数,当结果大于 1 时,使用 have 子句对其进行过滤。接下来选择帐户和日期作为输出列。

这是 SAS 对 SQL 的特定处理。大多数其他实现将不允许这种结构,您不会将 select 中的所有非聚合列放在 group by 子句中。

proc sql noprint;
    create table _output as
    select acct, date format=ddmmyys10.
    from _input
    group by acct
    having count(distinct date) > 1
    order by acct, date;
   quit;
于 2014-05-30T06:24:02.277 回答
1

像这样的东西会起作用。如果尚未按帐户/日期对数据进行排序,则检查每一last.date行。如果第一last.date行不是 also last.acct,那么它是一组需要输出受访者的行。在这里,每个日期/帐户组合我只输出一行:

data want;
set have;
by acct date;
if (last.date) and not (last.acct) then do;
  flg=1;
  output;
end;
else if last.date and flg=1 then output;
else if first.acct then flg=0;
run;

如果您需要所有行,那么您需要将上述内容合并回原始行,或者您可以执行 DoW 循环:

data want;
do _n_=1 by 1 until (last.acct);
 set have;
 by acct date;
 if (last.date) and not (last.acct) then do;
  flg=1;
 end;
end;
do _t_ = 1 by 1 until (last.acct);
 set have;
 by acct date;
 if flg=1 then output;
end;
run;
于 2014-05-29T19:23:25.677 回答