Calculating business days

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating business days

Sea, Carleton, VBAVACO
Calculating business days

I am looking to calculate the number of days between 2 date variables.  However, I need to calculate the business days (M-F) rather than calendar days.

Does anyone have any ideas?  Any help would be appreciated.

V/r,



Carleton Sea

Reply | Threaded
Open this post in threaded view
|

Re: Calculating business days

Jon K Peck
Simplest way is to use programmability.  Assuming that you have installed the Python Essentials and downloaded the extendedTransforms.py module and the SPSSINC TRANS extension command from the SPSS Community website (www.ibm.com/developerworks/spssdevcentral), you could use this code.

Illustrated using the employee data.sav dataset shipped with Statistics.

First create a second date variable a random number of days after birth date.

select if ~missing(bdate).
COMPUTE bdateplus=datesum(bdate, rv.poisson(10), "days").
format bdateplus(adate10).

Now do the calculation.

spssinc trans result=wkdays
/formula "extendedTransforms.countWkdays(bdate, bdateplus)".

This function assumes that week days are Mon-Fri.  It has an optional parameter for other definitions.

HTH,



Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        "Sea, Carleton, VBAVACO" <[hidden email]>
To:        [hidden email]
Date:        04/17/2012 08:10 AM
Subject:        [SPSSX-L] Calculating business days
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I am looking to calculate the number of days between 2 date variables.  However, I need to calculate the business days (M-F) rather than calendar days.

Does anyone have any ideas?  Any help would be appreciated.

V/r,


Carleton Sea

Reply | Threaded
Open this post in threaded view
|

Re: Calculating business days

David Marso
Administrator
In reply to this post by Sea, Carleton, VBAVACO
Something like:
COMPUTE Days=CTIME.DAYS(date2-date1).
*gives you number of days.
COMPUTE Weeks=Days/7.
*gives you number of weeks.
COMPUTE Workdays=Weeks*5.
---------------------------------
But there are holidays and leftovers, so this is approximate at best.
COMPUTE Workdays=CTIME.DAYS(date2-date1)*5/7.
*An exact evaluation would involve somewhat trickier code.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Calculating business days

Maguin, Eugene
In reply to this post by Sea, Carleton, VBAVACO
Calculating business days

Let date1 and date2 be the two dates.

Compute businessdays=0.

Compute #tdate=date1.

Set mxloops=100. /* make sure this number is greater than the date2-date1+1.

Loop .

+  if (xdate.wkday(#tdate) ge 2 and xdate.wkday(date1) le 6) businessdays=businessdays+1.

Compute #tdate=datesum(date1,1,"days").

End loop If (#tdate gt date2).

 

Gene Maguin

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Sea, Carleton, VBAVACO
Sent: Tuesday, April 17, 2012 9:52 AM
To: [hidden email]
Subject: Calculating business days

 

I am looking to calculate the number of days between 2 date variables.  However, I need to calculate the business days (M-F) rather than calendar days.

Does anyone have any ideas?  Any help would be appreciated.

V/r,

 

Carleton Sea

Reply | Threaded
Open this post in threaded view
|

Re: Calculating business days

David Marso
Administrator
LOOP #D=date1 TO date2 BY 86400.
+  COMPUTE businessdays=SUM(businessdays,RANGE(XDATE.WKDAY(#D),2,6)).
END LOOP.
;-))

Maguin, Eugene wrote
Let date1 and date2 be the two dates.
Compute businessdays=0.
Compute #tdate=date1.
Set mxloops=100. /* make sure this number is greater than the date2-date1+1.
Loop .
+  if (xdate.wkday(#tdate) ge 2 and xdate.wkday(date1) le 6) businessdays=businessdays+1.
Compute #tdate=datesum(date1,1,"days").
End loop If (#tdate gt date2).

Gene Maguin

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Sea, Carleton, VBAVACO
Sent: Tuesday, April 17, 2012 9:52 AM
To: [hidden email]
Subject: Calculating business days


I am looking to calculate the number of days between 2 date variables.  However, I need to calculate the business days (M-F) rather than calendar days.

Does anyone have any ideas?  Any help would be appreciated.

V/r,


Carleton Sea
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Calculating business days

Bruce Weaver
Administrator
Look Ma, no Python!  ;-)


David Marso wrote
LOOP #D=date1 TO date2 BY 86400.
+  COMPUTE businessdays=SUM(businessdays,RANGE(XDATE.WKDAY(#D),2,6)).
END LOOP.
;-))

Maguin, Eugene wrote
Let date1 and date2 be the two dates.
Compute businessdays=0.
Compute #tdate=date1.
Set mxloops=100. /* make sure this number is greater than the date2-date1+1.
Loop .
+  if (xdate.wkday(#tdate) ge 2 and xdate.wkday(date1) le 6) businessdays=businessdays+1.
Compute #tdate=datesum(date1,1,"days").
End loop If (#tdate gt date2).

Gene Maguin

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Sea, Carleton, VBAVACO
Sent: Tuesday, April 17, 2012 9:52 AM
To: [hidden email]
Subject: Calculating business days


I am looking to calculate the number of days between 2 date variables.  However, I need to calculate the business days (M-F) rather than calendar days.

Does anyone have any ideas?  Any help would be appreciated.

V/r,


Carleton Sea
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).