## Fast way to calculate time in minutes over two days

Frequent Contributor
Posts: 78

# Fast way to calculate time in minutes over two days

I have a huge dataset in which a procedure was started at 23:00 and finished at 02:00 (for example) and I need to calculate how many minutes for the procedure. Every method I use gives me negative values.

Any suggestions? I've searched the internet for help with no success so far.

Super User
Posts: 3,927

## Re: Fast way to calculate time in minutes over two days

Does your data also record a start date and finish date? If so then you can combine the dates and times to get what you want like so:

start date time = dhms(start date,start hh,start mm, start ss);
end date time = dhms(end date,end hh,e nd mm, end ss);
duration = end date time - start date time;
format duration datetime. ;

If you don't have start and finish dates then its a bit harder. Can a procedure last more than one day? If yes then you have a problem! If not then you can assume if end is less than start then it has gone over just one day boundary:

if end time < start time then duration = end time + 24 - start time;
else duration = end time - start time;
Super Contributor
Posts: 3,176

## Re: Fast way to calculate time in minutes over two days

Have a look at the INTCK function while more importantly working with SAS DATETIME variables in your computation. SAS Language DOC and the SAS support website have reference material and SAS coding examples -search on these terms.

Scott Barry
SBBWorks, Inc.

data step datetime variables intck site:sas.com
Valued Guide
Posts: 2,191

## Re: Fast way to calculate time in minutes over two days

rather than use time values, use datetime() function to indicate a point in time. The difference of the values returned from the datetime() function will provide a duration in seconds that will present well with format mmss11.2
%let starting_at= %sysfunc( datetime(), 20.3) ;
* your long procedure follows ;
%let finishing_at = %sysfunc( datetime(), 20.3) ;

%put duration of long procedure was %sysfunc( range( &starting_at, &finishing_at), mmss11.2 ) ;

Demonstrated in this clip from a SASlog[pre]1 %let starting_at= %sysfunc( datetime(), 20.3) ;
2 * your long procedure follows ;
3 %put waiting %sysfunc( sleep(1234, .001 ));
waiting 0.001
4 %let finishing_at = %sysfunc( datetime(), 20.3) ;
5
6 %put duration of long procedure was %sysfunc( range( &starting_at, &finishing_at), mmss11.2 ) ;
duration of long procedure was 0:01.31[/pre]
Super User
Posts: 10,787

## Re: Fast way to calculate time in minutes over two days

Why not use function abs(), which will always give you a positive value!
Frequent Contributor
Posts: 78

## Re: Fast way to calculate time in minutes over two days

Thanks to everyone for your suggestions.

I finally figured out something last night before I had a chance to check back here. I just used a calculation.

timeactive=(('24:00't-ltime)+ltime2)/60;

ltime=the time before midnight
ltime2= the time after midnight.

I decided not to incorporate the dates since the time span should never be longer than 8 hours, so there will only be two days involved.

I will consider datetime in the future if it will span more days.
Discussion stats
• 5 replies
• 308 views
• 0 likes
• 5 in conversation