data wanted;
set test;
by id;
retain flag2;
if first.id then call missing(flag1,flag2);
flag1+1;
if flw_status in (3,4) then flag2=flag1;
if flag1>flag2>. then delete;
drop flag:;
run;
data wanted;
set test;
by id;
if first.id then call missing(flag);
if flw_status in (3,4) then flag+1;
if (flag=1 and flw_status=1) or flag>1 then delete;
drop flag;
run;
data wanted;
set test;
by id;
if first.id then call missing(flag1,flag2);
if flw_status in (3,4) then flag1+1;
if flag1 ne . then flag2+1;
if flag2>1 then delete;
drop flag:;
run;