turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- BI
- /
- Enterprise Guide
- /
- Difference between two time periods

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 02:37 PM

I would like to calculate difference between two time periods in days Hours, minutes ( how long it takes to complete the task)

for example difference between 12MAY2014:08:20:19:00 and 07MAY2014:08:00:45:00

Please help

Thanks,

Accepted Solutions

Solution

05-30-2014
10:49 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-30-2014 10:49 AM

data x; t1 = '12MAY2014:08:20:19.00'dt; t2 = '07MAY2014:08:00:45.00'dt; days = int((t1-t2)/'24:00:00't); time= mod((t1-t2),'24:00:00't); format time time8. t1 t2 datetime.; run;

Xia Keshan

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 03:07 PM

Subtract the two records will give you the time in seconds. Then you can convert that to minutes/hours with some basic math (ie divide by 60). You can also try formatting using various time formats but because you cross days that might be an issue.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 03:12 PM

I did subtract the two . However, I like to get the results in Days Hours using format statement or functions?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 03:14 PM

How would you like that to look? I don't believe there's a standard way to indicate days for duration in standard format.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 03:10 PM

Take the difference between the two, the result is a number of seconds, then express in the required units:

**data _null_;**

**t1 = '12MAY2014:08:20:19.00'dt;**

**t2 = '07MAY2014:08:00:45.00'dt;**

**dt = t1 - t2; /* time expressed in seconds */**

**dtStr = catx(":", put(dt/(24*60*60),6.0), put(mod(abs(dt),24*60*60),hhmm.));**

**put dt= dtStr=;**

**run;**

PG

Message was edited by: PG To get the sign right all the time.

PG

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 03:13 PM

Thanks. I will give this a try

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 06:29 PM

The intck function does this for you. You specify what interval you want e.g. days, hours

INTCK Function :: SAS(R) 9.4 Functions and CALL Routines: Reference, Second Edition

and the different intervals you can use:

http://support.sas.com/documentation/cdl/en/lrcon/67227/HTML/default/p0g056g35ez8son1sfavozh0lfb3.ht...http://support.sas.com/documentation/cdl/en/lefunctionsref/67239/HTML/default/viewer.htm#p1md4mx2crz...

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-29-2014 06:31 PM

Something like:

**data _null_;**

**t1 = '12MAY2014:08:20:19.00'dt;**

**t2 = '07MAY2014:08:00:45.00'dt;**

howlong = intck('second',t2,t1);

days = datepart(howlong); /* basically taken from Professional SAS Programming Secrets, dating back to V6.03 and page 366-367 */

partday = timepart(howlong);

put days 3. ':' partday time8.

**run;**

Solution

05-30-2014
10:49 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-30-2014 10:49 AM

data x; t1 = '12MAY2014:08:20:19.00'dt; t2 = '07MAY2014:08:00:45.00'dt; days = int((t1-t2)/'24:00:00't); time= mod((t1-t2),'24:00:00't); format time time8. t1 t2 datetime.; run;

Xia Keshan

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-30-2014 10:52 AM

Thanks for sample code. I will give it a try