%let n=3;
proc sql noprint;create table x_new as select *,count(*)/4 as d
from x group by code having d>&n ;
select * from x_new order by code , year desc;
create table final as select * from x_new group by code
having min(year)+&n<=year<=max(year) order code ,year desc;quit;
Here is one-step data step solution. It is assumed that data is sorted by code and year.
data a;
do code=1,2,3;
numyear=ceil(ranuni(567)*10)+3;
do i=1 to numyear;
year=2000+i;
output;
end;
end;
drop i numyear;
run;
data b;
n=0;
do until(last.code);
set a;
n+1;
by code;
end;
l=0;
do until(last.code);
set a;
l+1;
by code;
if l<=n-3 then output;
end;
keep code year;
run;