BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ebills
Fluorite | Level 6

Hello,

 

I'm trying to calculate the number of hours between two time variables in Proc SQL. That calculation is easy enough, but the obstacle I'm facing is the span between days. For instance, if an event happened on the 8th at 11:00pm and ended on the 9th at 4:00am, I want it to give me 5 hours. I have a beginning date (date9.) variable, an ending date (date9.) variable and the two (time10.) time variables (beginning/ending).

 

Would I need to aggregate these to create datetime variables and then proceed to do the intck calculation from there?

 

Any help is appreciated! Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

I would create a datetime variable using DHMS using your two variables and then subtract the two which would give me the difference in seconds.

 

Assuming SAS dates and time values:

dhms(date1, 0, 0, time1) - dhms(date2, 0, 0, time2) as duration_seconds

View solution in original post

3 REPLIES 3
Reeza
Super User

I would create a datetime variable using DHMS using your two variables and then subtract the two which would give me the difference in seconds.

 

Assuming SAS dates and time values:

dhms(date1, 0, 0, time1) - dhms(date2, 0, 0, time2) as duration_seconds
ebills
Fluorite | Level 6

Thanks Reeza. This got me what I wanted.

 

dhms(event_end_date,0,0,(input(event_end_time, time.))) - dhms(event_begin_date,0,0,(input(event_begin_time, time.)))
format =time8. as duration,

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 4092 views
  • 0 likes
  • 3 in conversation