data have;
informat
id $1.
indate ddmmyy10.
outdate ddmmyy10.
;
format
id $1.
indate ddmmyy10.
outdate ddmmyy10.
;
input id indate outdate intime outtime;
cards;
X 15/05/2013 15/05/2013 13 16
X 16/05/2013 16/05/2013 1 1
Y 24/12/2014 25/12/2014 14 16
Z 02/12/2011 03/12/2011 16 23
Z 04/12/2011 05/12/2011 2 17
Z 06/12/2011 06/12/2011 1 8
W 07/08/2012 08/08/2012 14 20
M 01/01/2012 02/01/2012 16 8
M 02/01/2012 03/01/2012 9 16
;
run;
proc sort data=have;
by id indate intime;
run;
data want;
set have;
by id;
retain numberlines;
format
indate_dt
lastoutdate_dt
datetime19.
;
lastoutdate_dt = dhms(lag(outdate),lag(outtime),0,0);
if first.id then lastoutdate_dt = 0;
indate_dt = dhms(indate,intime,0,0);
if intck('hour',lastoutdate_dt,indate_dt) < 12
then numberlines + 1;
else numberlines = 1;
run;
If you need the original order, you would need to preserve that with setting a variable to _n_ in the first step and sorting the want dataset by that variable.
... View more