data a;
input num t x @;
cards;
1 78 4
1 79 9
1 82 3
1 83 7
2 79 8
2 80 2
2 84 5
2 81 5
3 81 4
3 82 4
3 83 5
3 89 6
3 84 7
;
run;
proc sort data=a out=b;
by num t;
run;
proc sql;
create table c as select num, min(t) as t,max(t)-min(t) as no from b group by num;
quit;
data d;
set c;
if num=1 then do;
do i=1 to 5;
output;
t+1;
end;
end;
else if num=2 then do;
do i=1 to 5;
output;
t+1;
end;
end;
else do;
do i=1 to 8;
output;
t+1;
end;
end;
drop no i;
run;
data f;
merge b d;
by num t;
if x=. then x=0;
run;
data a;
input num t x @;
cards;
1 78 4
1 79 9
1 82 3
1 83 7
2 79 8
2 80 2
2 84 5
2 81 5
3 81 4
3 82 4
3 83 5
3 89 6
3 84 7
;
run; proc sql;
create table tmp as select num ,t from a
group by num having t=min(t) or t=max(t) order num ,t;quit;
data y;
set tmp (firstobs=2 rename=(t=end)) end=last nobs=lastrec;
set tmp (rename=(t=start));
do t=start to end-1;
output;
end;drop end start;
run;
proc sort data=a;by num t;run;
proc sort data=y;by num t;run;
data final;merge a y;by num t;if x=. then x=0;run;