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.
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;
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.
Suggested Google advanced search argument, this topic/post:
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 ));
4 %let finishing_at = %sysfunc( datetime(), 20.3) ;
6 %put duration of long procedure was %sysfunc( range( &starting_at, &finishing_at), mmss11.2 ) ;
duration of long procedure was 0:01.31[/pre]