Desktop productivity for business analysts and programmers

How to calculate time difference (minutes) between two datetime fields

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

How to calculate time difference (minutes) between two datetime fields

I have two datetime fields and want to calculate the difference in minutes between the two fields. I don't believe the DATDIF function will return minutes, will it? Is there another function that I can use? So far I haven't had any luck searching the SAS EG help or the web....

Thanks in advance.

Accepted Solutions
Solution
‎07-26-2016 01:33 PM
Valued Guide
Posts: 2,111

Re: How to calculate time difference (minutes) between two datetime fields

[ Edited ]

mdavidson wrote:
got it: INTCK('minute',field1,field2 )

 

INTCK can give results that aren't what you want in all cases, as it counts from the beginning of the interval (see the Details section of the documentation). If your data are recorded to the minute, it won't matter. If your data are recorded to the second, it might.

As datetime variables are recorded in seconds, an alternative is to use the fact that there are 60 seconds in a minutes and just do the arithmetic directly

(datetime2 - datetime1)/60 provides minutes with decimal fractions.
FLOOR( (datetime2 - datetime1)/60 ) provides the whole minute durations.

For instance, 12:01:50 to 12:02:10 is 1 using INTCK, 0.333 using the decimal minutes, and 0 using the FLOOR version.

View solution in original post


All Replies
Contributor
Posts: 53

Re: How to calculate time difference (minutes) between two datetime fields

Got it: INTCK('minute',field1,field2 )
Solution
‎07-26-2016 01:33 PM
Valued Guide
Posts: 2,111

Re: How to calculate time difference (minutes) between two datetime fields

[ Edited ]

mdavidson wrote:
got it: INTCK('minute',field1,field2 )

 

INTCK can give results that aren't what you want in all cases, as it counts from the beginning of the interval (see the Details section of the documentation). If your data are recorded to the minute, it won't matter. If your data are recorded to the second, it might.

As datetime variables are recorded in seconds, an alternative is to use the fact that there are 60 seconds in a minutes and just do the arithmetic directly

(datetime2 - datetime1)/60 provides minutes with decimal fractions.
FLOOR( (datetime2 - datetime1)/60 ) provides the whole minute durations.

For instance, 12:01:50 to 12:02:10 is 1 using INTCK, 0.333 using the decimal minutes, and 0 using the FLOOR version.

New Contributor
Posts: 2

Re: How to calculate time difference (minutes) between two datetime fields

This is exactly what I wanted. Thanks very much!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 22787 views
  • 4 likes
  • 3 in conversation