Hi, This is a continuation of the following threads, the second one was unanswered. I have the following data and code (a different scenario). The fake data is just for illustration. The goal is to compute the duration by name and by date. In this example Beth works midnight till 7:30 am and come back to work 9:00 pm till midnight the same day. The gap is then between 7:30 am and 9:00 pm. The program works for Alex that has a different work schedule (no gap during the day, 7:30-3:30 pm). The program did not give me the expected result for Beth . DATA fake; LENGTH status $ 6; INPUT @1 dt:datetime16. @18 name$ format dt datetime18.; date = datepart(dt); time = timepart(dt); datalines; 04Jan11:00:21:12 Beth 04Jan11:02:19:06 Beth 04Jan11:05:00:13 Beth 04Jan11:07:12:11 Beth 04Jan11:21:10:25 Beth 04Jan11:23:26:13 Beth 05Jan11:00:23:51 Beth 05Jan11:01:30:10 Beth 05Jan11:03:12:06 Beth 05Jan11:05:06:15 Beth 05Jan11:07:23:11 Beth 05Jan11:21:30:25 Beth 05Jan11:23:28:15 Beth 04Jan11:07:43:00 Alex 04Jan11:08:12:10 Alex 04Jan11:09:40:00 Alex 04Jan11:11:20:16 Alex 04Jan11:12:25:14 Alex 04Jan11:14:20:07 Alex ; run; proc sort; by name date time; data new; format date date9. time time8.; set fake; by name date time; if first.date then do; if (minute(dt)>30) then newdate=dhms(date,hour(dt),0,1800); if (minute(dt)<=30) then newdate=dhms(date,hour(dt),0,0); end; if last.date then do; if (minute(dt)>30) then newdate=dhms(date,hour(dt),0,3600); if (minute(dt)<=30) then newdate=dhms(date,hour(dt),0,1800); end; format newdate datetime16.; run; proc means data=new nway noprint; class date name; format newdate dtdate9.; output out=want (drop=_:) range(newdate)=duration; run; data want_output; set want; duration_time=duration; duration=duration/60/60; format duration; format duration_time hhmm8.2 date date9.; run; The resulting output is 04JAN2011 Alex 7 7:00.00 04JAN2011 Beth 23.5 23:30.00 05JAN2011 Beth 23.5 23:30.00 The desired output is 04JAN2011 Alex 7 7:00.00 04JAN2011 Beth 10.5 10:30.00 05JAN2011 Beth 10.5 10:30.00 There are wonderful helpers in this site. I appreciate any help. Thanks.
... View more