data want;
set have;
by studyid;
if first.studyid
then do;
_have = plt;
if dontype = 1
then have = _have;
else have = .;
end;
else do;
if dontype = 1
then do;
_have + 1; /* sum statement causes automatic retain */
have = _have;
end;
else have = .;
end;
drop _have;
run;
The LAG() can't be reasonably used with a newly created variable; it fills its queue every time it is called, and at that moment the variable is still missing, or has the previous value anyway, because of the RETAIN.
... View more