An idea:
it seems as if all obs having flag = "RECEIVED - LATE" are used as lookup, so the first step is creating a dataset having only those obs while keeping only id and date, renaming date to finish_date seems to be a good idea, too
using the created lookup dataset as hash object, keys are id and finish_date
if flag1 = 1 and flag2 = 2 a loop is started searching the hash object. In each iteration months_to_receive is increased by 1 until a date was found.
The step is hardly tested:
data want;
set have;
if _n_= 1 then do;
if 0 then set lookup;
declare hash h(dataset: 'lookup');
h.defineKey('id', 'finish_date');
h.defineDone();
end;
months_to_receive = 0;
finish_date = .;
if flag1 = 1 and flag2 = 2 then do;
months_to_receive = 1;
do until (not missing(finish_date));
finish_date = intnx('month', date, months_to_receive, 'b');
put finish_date=;
if h.check () ^= 0 then do;
finish_date = .;
months_to_receive = months_to_receive + 1;
end;
end;
end;
run;
... View more