data have;
input ID SIG $20.;
cards;
1 正常
1 正常
1 异常有临床意义
1 异常有临床意义
1 异常无临床意义
1 正常
1 正常
2 正常
2 正常
2 异常有临床意义
2 正常
2 正常
2 正常
2 异常有临床意义
2 正常
;run;
data want_1;
set have;
by id;
if first.id then do;
rows=0;
flg=0;
end;
rows+1;
if sig='异常有临床意义' then flg+1;
if flg>0;
run;
proc sql ;
create table work.want_2 as select t1.id,min(t1.rows) as normal_min_rows
from work.want_1 t1
,(select id,max(rows) as max_rows
from work.want_1
where sig='异常有临床意义'
group by id) t2
where t1.id=t2.id
and t1.rows>t2.max_rows
and t1.sig='正常'
group by t1.id;
quit;
data want;
merge work.want_1 work.want_2;
by id;
if rows<=normal_min_rows;
keep id sig;
run;
retain csfn; *此前存在cs记录的标识;
if first.id then csfn = .;
if sig = '异常有临床意义' then csfn = 1;
do i = rec to 1 by -1; *此前cs记录已经转变为正常的标识;
set have(rename=(id=tmpid sig=tmpsig)) nobs=rec point=i;
if id=tmpid and tmpsig = '异常有临床意义' then leave;
else if id=tmpid and tmpsig = '正常' and _N_>i then csfn = .;
end;
run;