BookmarkSubscribeRSS Feed
statadm
Fluorite | Level 6
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.

Thanks in advance!
5 REPLIES 5
SASKiwi
PROC Star
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;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.

Suggested Google advanced search argument, this topic/post:

data step datetime variables intck site:sas.com
Peter_C
Rhodochrosite | Level 12
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]
Ksharp
Super User
Why not use function abs(), which will always give you a positive value!
statadm
Fluorite | Level 6
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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1747 views
  • 0 likes
  • 5 in conversation