DATA Step, Macro, Functions and more

Number of minutes between two SAS datetime values (based on working week/hours)

Reply
Frequent Contributor
Posts: 75

Number of minutes between two SAS datetime values (based on working week/hours)

Hi,

I was wondering if there was a tidy function that would give me the number of minutes between two SAS datetime values considering the times from Monday to Friday and 9am to 5pm?

Eg, the number of minutes I want for 27JAN2015:09:20:00.000 and 26JAN2015:16:50:00.000 should be 30 minutes.

I think eventually I'd want to also exclude holidays from that, but that enhancement can be done later.

Thanks.

Super User
Posts: 10,046

Re: Number of minutes between two SAS datetime values (based on working week/hours)

data _null_;

do i= '26JAN2015:16:50:00.000'dt to '27JAN2015:09:20:00.000'dt;

  if weekday(datepart(i)) in (2:6) and 9 <= hour(timepart(i)) < 17 then seconds+1;

end;

minutes=divide(seconds,60);

put minutes=;

run;

Xia Keshan

Message was edited by: xia keshan

Respected Advisor
Posts: 3,156

Re: Number of minutes between two SAS datetime values (based on working week/hours)

I am not sure if I have understood what you asked for, this will give you the minute difference without regarding days and hours. What is your purpose? what is downstream application of the data?

data test;

     a="27JAN2015:09:20:00.000"dt;

     b="26JAN2015:16:50:00.000"dt;

     c=mod(intck('minute', b,a),60);

/*or c=abs(minute(b)-minute(a));*/

run;

Frequent Contributor
Posts: 75

Re: Number of minutes between two SAS datetime values (based on working week/hours)

The purpose of the function/step is to calculate the number of working minutes spent on a task for an employee.  There are SLAs/targets, and to be fair, the weekend shouldn't contribute towards the time spent, nor should the time outside of working hours be included either.


I think my test case was too simplistic.


I probably should have had it span a weekend, or a fortnight.


Okay, so if we compare 03FEB2015:09:20:00.000 and 30JAN2015:16:50:00.000 should be (17-9) * 60 + 20 + 10 = 510.


I've tested the supplied code, one of them looks to work (though there is a tiny offset):

16         %let dt_str = '30JAN2015:16:50:00.000'dt;
17         %let dt_end = '03FEB2015:09:20:00.000'dt;
18        
19         data hai_kuo;
20            a=&dt_end.;
21            b=&dt_str.;
22            c=mod(intck('minute', b,a),60);
23            d=abs(minute(b)-minute(a));
24        
25            put c= d=;
26         run;

c=
30 d=30
NOTE: The data set WORK.HAI_KUO has
1 observations and 4 variables.
NOTE: Compressing data set WORK.HAI_KUO increased size by
100.00 percent.
      Compressed is
2 pages; un-compressed would require 1 pages.
NOTE: MVA_DSIO.OPEN_CLOSE| _DISARM|         STOP| _DISARM|
2015-02-06T08:44:29,434+11:00| _DISARM| WorkspaceServer| _DISARM| SAS|
      _DISARM| | _DISARM| | _DISARM|
14991360| _DISARM| 11| _DISARM| 16| _DISARM| 263433| _DISARM| 96476329| _DISARM| 0.000000|
      _DISARM|
0.000000| _DISARM| 1738791869.434000| _DISARM| 1738791869.434000| _DISARM| 0.000000| _DISARM| | _ENDDISARM
NOTE: PROCEDURE| _DISARM|         STOP| _DISARM|
2015-02-06T08:44:29,434+11:00| _DISARM| WorkspaceServer| _DISARM| SAS| _DISARM| |
      _DISARM|
15257600| _DISARM| 14991360| _DISARM| 11| _DISARM| 16| _DISARM| 264440| _DISARM| 96476596| _DISARM| 0.000000|
      _DISARM|
0.000000| _DISARM| 1738791869.434000| _DISARM| 1738791869.434000| _DISARM| 0.000000| _DISARM| | _ENDDISARM
NOTE: DATA statement used (Total process time):
      real time          
0.01 seconds
      cpu time           
0.01 seconds
     

27        
28        
29         data xia_keshan;
30            do i= &dt_str. to &dt_end.;
31               if weekday(datepart(i)) in (2:6) and 9 <= hour(timepart(i)) < 17 then seconds+1;
32            end;
33        
34            minutes=divide(seconds,60);
35            put minutes=;
36         run;

minutes=
510.01666667

The second one works, it looks as though it's incrementing based on seconds.  If the periods are really far apart the job will take ages to run, eg 50 years.  I need to think about whether this is really what I want or not, but I think it does work !

Ask a Question
Discussion stats
  • 3 replies
  • 652 views
  • 3 likes
  • 3 in conversation