BookmarkSubscribeRSS Feed
tparvaiz
Obsidian | Level 7

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

3 REPLIES 3
art297
Opal | Level 21

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;

Astounding
PROC Star

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.

Haikuo
Onyx | Level 15

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 782 views
  • 0 likes
  • 4 in conversation