data try2 (DROP = LAG:);
set try1 ;
LAGfirst_cz=lag(FIRST_CZ) ;
LAGnext_cz=lag(NEXT_CZ);
LAG2first_cz=lag2(FIRST_CZ) ;
LAG2next_cz=lag2(NEXT_CZ);
if (LAGFIRST_CZ>0 and FIRST_CZ=.) then do;
first_cz=lagFIRST_CZ ;
next_cz=lagNEXT_CZ;
end; else if (LAGFIRST_CZ=. and LAG2FIRST_CZ>0 and FIRST_CZ=.) then do;
first_cz=lag2FIRST_CZ ;
next_cz=lag2NEXT_CZ;
end;
The reason that your code didn't work is that the LAG is an executable statement. To be useful, it should rarely be within a DO-END block. You can generalize buckeye's code, though I must say that the output in your second post does not follow a discernible pattern.
It looks like you pattern is not very clear. Anyway this is how I understood it:
[pre]
data i;
input rec FIRST_CZ NEXT_CZ;
datalines;
1 . .
2 . .
3 3 5
4 . .
5 . .
6 6 10
7 . .
8 . .
run;
data r;
retain f0 n0;
set i;
f=LAG(FIRST_CZ);
n=LAG(NEXT_CZ);
if f ne . and n ne . then do;
f0=f;
n0=n;
end;
if FIRST_CZ = . then FIRST_CZ=f0;
if NEXT_CZ = . then Next_CZ =n0;
drop n n0 f f0;
run;
[/pre]
Sincerely,
SPR