The final codes. data temp1; set temp;
by id dttm;
if whichn(1, of var:) and 0=whichn(0, of var:) then outcome=1;
else if 0=whichn(1, of var:) and whichn(0, of var:) then outcome=0;
else if first.id then outcome=0;
else outcome= not lag_outcome;
output;
if last.dttm then lag_outcome=outcome;
if last.id then lag_outcome=.;
retain lag_outcome;
run;
... View more