data test1;
set test;
obs=_n_;
if f4 ne lag(f4) then num+1;
run;
proc sql;
create table test2 as
select *, count(*) as n
from test1
group by num
having n=1 or f4=0
order by obs;
quit;
data _null_;
set test2(drop=num n);
lagobs=lag(obs);
if _n_>1 and obs-lagobs ne 1;
datnum+1;
call execute('data wanted'||cats(datnum)||';set test1;where obs>'||cats(lagobs)||' and obs<'||cats(obs)||';keep f1-f4;run;');
run;