Hi,
I have a data set with event details as follows
Event Name Location Start End
ABC Loc A 01Jan2012:10:0:0 01Jan2012:12:0:0
DEF Loc B 02Jan2012:08:0:0 02Jan2012:11:0:0
GHI Loc B 02Jan2012:18:0:0 02Jan2012:20:0:0
…
I want to know the time difference b.w end of the event and the start of the next event… something like this
Event Name Location Start End Next_Event_In_Hrs
ABC Loc A 01Jan2012:10:0:0 01Jan2012:12:0:0 20
DEF Loc B 02Jan2012:08:0:0 02Jan2012:11:0:0 7
GHI Loc B 02Jan2012:18:0:0 02Jan2012:20:0:0 -
Thanks in advance
You could use something like:
data want (drop=next);
set have;
set have (firstobs=2 drop=Event_Name Location End rename=start=next)
have (obs=1 drop=_all_);
time_between=(next-start)/(60*60);
run;
Assuming your START and END values are SAS datetime values, not character strings:
data want;
set have end=last_one;
if not last_one then do;
set have (firstobs=2 keep=start rename=(start=next_starting_time));
next_event_in_hrs = (next_starting_time - end) / 3600;
end;
drop next_starting_time;
run;
Good luck.
Here is some undocumented SQL,
data have;
input Event_Name$ Location:$&8. (Start End) (:datetime30.);
format start end DATETIME30.;
cards;
ABC Loc A 01Jan2012:10:0:0 01Jan2012:12:0:0
DEF Loc B 02Jan2012:08:0:0 02Jan2012:11:0:0
GHI Loc B 02Jan2012:18:0:0 02Jan2012:20:0:0
;
proc sql;
select a.*, intck('hour', a.end,b.start) as Next_Event_In_Hrs
from (select *, monotonic() as _ano from have) a
left join
(select *, monotonic() as _bno from have) b
on a._ano+1=b._bno;
quit;
Haikuo
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.