问题描述和给出的示例前后矛盾,如果是到前三,那么id 2和3的结尾错误。参考附件代码,可根据需要修改判断index
- data raw;
- length date $10;
- input id date$ happen;
- datalines;
- 1 2001/1/1 0
- 1 2001/1/2 0
- 1 2001/1/3 0
- 1 2001/1/4 0
- 1 2001/1/5 0
- 1 2001/1/6 0
- 1 2001/1/7 0
- 1 2001/1/8 0
- 1 2001/1/9 0
- 1 2001/1/10 0
- 1 2001/1/11 0
- 1 2001/1/12 0
- 1 2001/1/13 1
- 1 2001/1/14 0
- 1 2001/1/15 0
- 1 2001/1/16 0
- 1 2001/1/17 0
- 1 2001/1/18 0
- 2 2003/3/2 0
- 2 2003/3/3 0
- 2 2003/3/4 0
- 2 2003/3/5 0
- 2 2003/3/6 0
- 2 2003/3/7 0
- 2 2003/3/8 0
- 2 2003/3/9 0
- 2 2003/3/10 0
- 2 2003/3/11 0
- 2 2003/3/12 0
- 2 2003/3/13 1
- 2 2003/3/14 0
- 2 2003/3/15 0
- 3 2002/8/2 0
- 3 2002/8/3 0
- 3 2002/8/4 0
- 3 2002/8/5 0
- 3 2002/8/6 0
- 3 2002/8/7 0
- 3 2002/8/8 0
- 3 2002/8/9 0
- 3 2002/8/10 0
- 3 2002/8/11 0
- 3 2002/8/12 0
- 3 2002/8/13 1
- 3 2002/8/14 0
- 3 2002/8/15 0
- 3 2002/8/16 0
- ;
- run;
- data raw1;
- set raw;
- by id;
- retain _in_id;
- if first.id then _in_id=0;
- _in_id+1;
- run;
- proc sql noprint;
- create table raw2 as
- select a.*,b._in_id as index from raw1 as a
- left join (select id,min(_in_id) as _in_id from raw1 where happen=1 group by id) as b
- on a.id eq b.id
- order by id,_in_id;
- quit;
- data wanted;
- set raw2;
- if index>10 then do;
- if _in_id<=10 and _in_id<=index-3 then output;
- end;
- else do;
- if _in_id eq index-1 then output;
- end;
- run;
复制代码