从下面的示例数据中,我正在尝试识别帐户(按ID和SEQ),其中至少连续3个月出现status_date。我一直搞乱这一段时间,我并不肯定如何解决它。

样本数据:

ID     SEQ   STATUS_DATE
11111    1   01/01/2014
11111    1   02/10/2014
11111    1   03/15/2014
11111    1   05/01/2014
11111    2   01/30/2014
22222    1   06/20/2014
22222    1   07/15/2014
22222    1   07/16/2014
22222    1   08/01/2014
22222    2   02/01/2014
22222    2   09/10/2014
.

我需要返回的内容:

ID      SEQ   STATUS_DATE
11111    1    01/01/2014
11111    1    02/10/2014
11111    1    03/15/2014
22222    1    06/20/2014
22222    1    07/15/2014
22222    1    07/16/2014
22222    1    08/01/2014
.

任何帮助都会受到赞赏。

有帮助吗?

解决方案

以下是一种方法:

data have;
input ID     SEQ   STATUS_DATE $12.;
datalines;
11111    1   01/01/2014
11111    1   02/10/2014
11111    1   03/15/2014
11111    1   05/01/2014
11111    2   01/30/2014
22222    1   06/20/2014
22222    1   07/15/2014
22222    1   07/16/2014
22222    1   08/01/2014
22222    2   02/01/2014
22222    2   09/10/2014
;
run;

data grouped (keep = id seq status_date group) groups (keep = group2);
    set have;
    sasdate = input(status_date, mmddyy12.);
    month = month(sasdate);
    year = year(sasdate);
    pdate = intnx('month', sasdate, -1);
    if lag(year) = year(sasdate) and lag(month) = month(sasdate) then group+0;
    else if lag(year) = year(pdate) and lag(month) = month(pdate) then count+1;
    else do;
        group+1;
        count = 0;
    end;
    if count = 0 and lag(count) > 1 then do;
        group2 = group-1;
        output groups;
    end;
    output grouped;
run;

data want (keep = id seq status_date);
    merge grouped groups (in=a rename=(group2=group));
    by group;
    if a;
run;
.

基本上我给出了相同的组编号如果连续几个月,那么也创建一个数据集,其中包含超过2个观察的组数。然后我合并这两个数据集,并且只保留在第二数据集中的观察,即具有超过2个观察的人。

其他提示

怎么样。但是,如果这就是你想要的那样,你可能想要排序

data want;
    do _n_ = 1 by 1 until(last.id);
    set survey;
    by id;
    if _n_ <=3 then output;
    end;
run;
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top