Assuming both have1 and have2 are sorted by id/date, then this interleaving operation will work:
data want;
set have2 (in=in2 rename=(date2=date1))
have1 (in=in1);
by id date1;
date2=ifn(in1 and lag(in2) and first.id=0,lag(date1),.);
if in1;
format date1 date2 datetime25.6;
run;
Notes:
In the SET statement HAVE2 must precede HAVE1 (for when dates are exactly tied)
The IFN function tests whether the record-in-hand is from have1, and is immediately preceded by a HAVE2 record for the same id. If true then the prior date1 value is really a date2 var that has an earlier date.
The IFN ALWAYS updates the lag queues, even if the lagged value is not returned by IFN.
... View more