I wasn't sure if different IDs should automatically be assigned different NEW_IDs. Your example NEW_IDs appear to work one way for some records and the other for others. Both approaches are done in the following examples.
proc sort data=have;
by ID Datetime1 Datetime2;
run;
data want;
set have;
by ID Datetime1 Datetime2;
retain NEW_ID2 0;
sep1 = intck('hour',lag(Datetime1),Datetime1);
sep2 = intck('hour',lag(Datetime2),Datetime2);
if first.ID then NEW_ID2 = rand('integer', 100, 999);
else if sep1 > 1 and sep2 > 1 then NEW_ID2 = rand('integer', 100, 999);
run;
proc sort data=have;
by Datetime1 Datetime2;
run;
data want2;
set have;
by Datetime1 Datetime2;
retain NEW_ID2 0;
sep1 = intck('hour',lag(Datetime1),Datetime1);
sep2 = intck('hour',lag(Datetime2),Datetime2);
if _n_ = 1 then NEW_ID2 = rand('integer', 100, 999);
else if sep1 > 1 and sep2 > 1 then NEW_ID2 = rand('integer', 100, 999);
run;
... View more