首页 > 解决方案 > SAS按组识别单品未按预期工作

问题描述

我正在尝试从某个组中删除汗衫。这是我的代码:

proc sort data=have; by ID date; run;
 
data want; 
    set have;
    by ID date;
    if first.date and last.date then delete;
run;

对我来说,这应该删除在同一 ID 中只有一个日期的所有条目。但由于某种原因,没有一个观察被允许通过。根据我的数据集,我应该显示数以千计的观察结果。我以前在其他数据集上使用过这个,没有任何问题。我知道这不是很多事情要做,但是我犯了一些我错过的错误吗?

标签: sas

解决方案


你在做什么:

data want; 
    set have;
    by ID date;
    if first.date and last.date then delete;   *if this is the first and last record for that date, then delete it;
run;

你想做什么,我相信:

data want; 
    set have;
    by ID date;
    if first.ID and last.ID then delete;  *if this is the only record for that ID then delete it;
run;

但是,这可能不是您想要的——取决于您的数据。你也有

proc sort nounikey data=have;
  by id;
run;

这将删除任何“唯一”记录。


推荐阅读