## Proc SQL: Calculate the number of hours between two time variables with separate date variables?

Solved
Occasional Contributor
Posts: 9

# Proc SQL: Calculate the number of hours between two time variables with separate date variables?

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!

Accepted Solutions
Solution
‎08-08-2016 05:23 PM
Super User
Posts: 23,771

## Re: Proc SQL: Calculate the number of hours between two time variables with separate date variables?

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``

All Replies
Super Contributor
Posts: 286

## Re: Proc SQL: Calculate the number of hours between two time variables with separate date variables?

Solution
‎08-08-2016 05:23 PM
Super User
Posts: 23,771

## Re: Proc SQL: Calculate the number of hours between two time variables with separate date variables?

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``
Occasional Contributor
Posts: 9

## Re: Proc SQL: Calculate the number of hours between two time variables with separate date variables?

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,

☑ This topic is solved.