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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

View solution in original post

9 REPLIES 9
Reeza
Super User

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.

avatar
Fluorite | Level 6

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

Reeza
Super User

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

PGStats
Opal | Level 21

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
avatar
Fluorite | Level 6

Thanks. I will give this a try

lloydc
Calcite | Level 5

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;

Ksharp
Super User
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

avatar
Fluorite | Level 6

Thanks  for sample code. I will give it  a try

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 9 replies
  • 3141 views
  • 1 like
  • 6 in conversation