data test2 (drop=a b);
set test1;
nm2=n-2; *n-2;
nm1=n-1;
nn=n;
np1=n+1;
np2=n+2;
if b ge 10;
run;
data test3;
set test2;
recordind=_n_;
if nm2 then do; n=nm2; output; end;
if nm1 then do; n=nm1; output; end;
if nn then do; n=nn; output;end;
if np1 then do; n=np1;output;end;
if np2 then do; n=np2;output;end;
run;
data test4 (keep=a b recordind);
merge test1 test3 (in =c);
by n;
if c;
run;
data test5 (keep=var1-var5);
set test4;
by recordind;
retain var1 var2 var3 var4 var5;
if first.recordind then m=0;
m+1;
if m=1 then var1=b;
if m=2 then var2=b;
if m=3 then var3=b;
if m=4 then var4=b;
if m=5 then var5=b;
if last.recordind;
run;
data test2 (drop=a b);
set test1;
nm2=n-2; *n-2;
nm1=n-1;
nn=n;
np1=n+1;
np2=n+2;
if b ge 10;
run;
data test3;
set test2;
recordind=_n_;
if nm2 then do; n=nm2; output; end;
if nm1 then do; n=nm1; output; end;
if nn then do; n=nn; output;end;
if np1 then do; n=np1;output;end;
if np2 then do; n=np2;output;end;
run;
data test4 (keep=a b recordind);
merge test1 test3 (in =c);
by n;
if c;
run;
data test5 (keep=var1-var5);
set test4;
by recordind;
retain var1 var2 var3 var4 var5;
if first.recordind then m=0;
m+1;
if m=1 then var1=b;
if m=2 then var2=b;
if m=3 then var3=b;
if m=4 then var4=b;
if m=5 then var5=b;
if last.recordind;
run;