質問

下記のサンプルデータから、私は(IDとSEQによって)アカウントを特定しようとしています。私はしばらくの間これをめちゃくちゃにしてきました、そして私はそれに取り組む方法はまったくありません。

サンプルデータ:

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
.

あらゆる助けが理解されるでしょう。

役に立ちましたか?

解決

これは1つの方法です:

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つのデータセットをマージし、2番目のデータセットにある観測値、つまり2つ以上の観測値を持つものだけを保持します。

他のヒント

次の方法はどうですか。しかし、あなたがあなたが欲しいものなら月に sort を述べたいかもしれません。

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