Calculating dates excluding weekend and holiday

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

Calculating dates excluding weekend and holiday

Chang,Victor
Dear SPSS experts:

I used the following syntax to calculate the duration between invstart and
invcomp:

COMPUTE    COMPDAY=CTIME.DAYS(INVCOMP-INTSTART).

     CASENO    INVSTART     INVCOMP  COMPDAY

   30331929 04-MAR-2009 02-SEP-2009   182.00
   30565269 20-MAR-2009 02-OCT-2009   196.00
   30594989 23-MAR-2009 19-OCT-2009   210.00
   30906990 11-APR-2009 28-SEP-2009   171.00
   31078069 22-APR-2009 22-SEP-2009   153.00
   31379029 11-MAY-2009 06-OCT-2009   148.00
   31554411 20-MAY-2009 07-OCT-2009   140.00
   31732009 01-JUN-2009 03-SEP-2009    94.00
   31737589 02-JUN-2009 22-SEP-2009   112.00
   31857209 09-JUN-2009 02-SEP-2009    85.00

Now, I was requested to exclude weekend and holiday from the calculation
(assuming 15-MAR-2009 as a holiday).  I tried but failed to get what I need.
Any one's advice on this solution is appreciated.  Someone of this
discussion group may have experienced this similar work and have developed a
good solution.

Regards,

Victor Chang

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Calculating dates excluding weekend and holiday

Bruce Weaver
Administrator
Chang,Victor wrote
Dear SPSS experts:

I used the following syntax to calculate the duration between invstart and
invcomp:

COMPUTE    COMPDAY=CTIME.DAYS(INVCOMP-INTSTART).

     CASENO    INVSTART     INVCOMP  COMPDAY

   30331929 04-MAR-2009 02-SEP-2009   182.00
   30565269 20-MAR-2009 02-OCT-2009   196.00
   30594989 23-MAR-2009 19-OCT-2009   210.00
   30906990 11-APR-2009 28-SEP-2009   171.00
   31078069 22-APR-2009 22-SEP-2009   153.00
   31379029 11-MAY-2009 06-OCT-2009   148.00
   31554411 20-MAY-2009 07-OCT-2009   140.00
   31732009 01-JUN-2009 03-SEP-2009    94.00
   31737589 02-JUN-2009 22-SEP-2009   112.00
   31857209 09-JUN-2009 02-SEP-2009    85.00

Now, I was requested to exclude weekend and holiday from the calculation
(assuming 15-MAR-2009 as a holiday).  I tried but failed to get what I need.
Any one's advice on this solution is appreciated.  Someone of this
discussion group may have experienced this similar work and have developed a
good solution.

Regards,

Victor Chang
There's probably some more elegant way to do it, but I think this works.

show mxloops. /* Default value is 40 on my PC .
set mxloops = 300. /* Set max # of loops to 300 .

numeric testdate(date11).
compute days = 0.
compute testdate = invstart.
loop.
- if not any(XDATE.WKDAY(testdate),1,7) and
  (testdate NE date.dmy(15,3,2009)) days = days + 1.
- compute testdate = testdate + (24*60*60). /* add one day .
end loop if testdate GT INVCOMP.
exe.

set mxloops = 40. /* Reset to default value.

--
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/).
Reply | Threaded
Open this post in threaded view
|

Vector Definition Issues

Mark Vande Kamp
In reply to this post by Chang,Victor
We are creating code that uses vectors and running into problems arising when the vector definition code is inconsistent with the presence or absence of the variables in the data file.

For example, when the vector format creates new variables we have no problem the first time we run the syntax. However, if we happen to save the file and then run the syntax again, the vector commands do not work and the subsequent loops don't work either.

We tried the kludgy method of defining each vector using both the "new variable" and "existing variable" formats, but that creates a bunch of error messages in the log that make it difficult to find legitimate errors.

I'm looking for syntax that will work whether or not the variables happen to be present in the data file.

Any help will be appreciated.

Mark

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Vector Definition Issues

Richard Ristow
At 02:26 PM 11/24/2009, Mark Vande Kamp wrote:

We are creating code that uses vectors and running into problems arising when the vector definition code is inconsistent with the presence or absence of the variables in the data file. For example, when the vector format creates new variables we have no problem the first time we run the syntax. However, if we happen to save the file and then run the syntax again, the vector commands do not work and the subsequent loops don't work either.

Here's a possible work-around, untested. In this example, the vector elements are Datum01 TO Datum17. It should work, because assigning to a numeric variable works whether or not the variable already exists. Then, it should always work with 'already-exists' vector syntax.

DO IF $CASENUM EQ 1.
.  DO REPEAT  Element = Datum01 TO Datum17.
.     COMPUTE Element = 0.
.  END REPEAT.
END IF.

VECTOR DATA=Datuum01 TO Datum17.

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Vector Definition Issues

Marks, Jim

Richard:

 

I thought a vector needed to refer to contiguous variables in a dataset? If some of the variables are in the data set and some are added at the end of the variable list, will this still work?

 

Jim Marks

Director, Market Research

x1616

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Richard Ristow
Sent: Tuesday, November 24, 2009 3:10 PM
To: [hidden email]
Subject: Re: Vector Definition Issues

 

At 02:26 PM 11/24/2009, Mark Vande Kamp wrote:


We are creating code that uses vectors and running into problems arising when the vector definition code is inconsistent with the presence or absence of the variables in the data file. For example, when the vector format creates new variables we have no problem the first time we run the syntax. However, if we happen to save the file and then run the syntax again, the vector commands do not work and the subsequent loops don't work either.


Here's a possible work-around, untested. In this example, the vector elements are Datum01 TO Datum17. It should work, because assigning to a numeric variable works whether or not the variable already exists. Then, it should always work with 'already-exists' vector syntax.

DO IF $CASENUM EQ 1.
.  DO REPEAT  Element = Datum01 TO Datum17.
.     COMPUTE Element = 0.
.  END REPEAT.
END IF.

VECTOR DATA=Datuum01 TO Datum17.

 

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Re: Vector Definition Issues

Richard Ristow
At 04:42 PM 11/24/2009, Marks, Jim wrote:

>I thought a vector needed to refer to contiguous variables in a dataset?

Which, indeed, it does.

>If some of the variables are in the data set and some are added at
>the end of the variable list, will this still work?

Well, the only place you can add variables to a dataset is at the
end. So, the only way you can have pre-existing and new variables in
the same vector, is to have the pre-existing ones at the end of the dataset.

But, was that your problem? You wrote as if you're creating a
contiguous set of variables ("when the vector format creates new
variables we have no problem the first time we run the syntax"), and
were looking for syntax that would create these variables if
necessary, and define them as a vector whether newly created or not.
That was the problem I was addressing.

Tell us more about what you're trying to do: create a contiguous set
of variables, once? Create a contiguous set of variables, and then
extend the set on successive runs? Or, what else?

-Best wishes,
  Richard Ristow

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Vector Definition Issues

Mark Vande Kamp
It should be noted that _Jim Marks_ wrote the follow-up question, whereas I
wrote the initial post asking for help. I don't have a problem with contiguity
(sp?). In fact, I just tried the syntax and it should work perfectly for our situation.

Thanks, Richard!

Mark Vande Kamp


 On Tue, 24 Nov 2009, Richard Ristow wrote:

> At 04:42 PM 11/24/2009, Marks, Jim wrote:
>
>> I thought a vector needed to refer to contiguous variables in a dataset?
>
> Which, indeed, it does.
>
>> If some of the variables are in the data set and some are added at
>> the end of the variable list, will this still work?
>
> Well, the only place you can add variables to a dataset is at the
> end. So, the only way you can have pre-existing and new variables in
> the same vector, is to have the pre-existing ones at the end of the dataset.
>
> But, was that your problem? You wrote as if you're creating a
> contiguous set of variables ("when the vector format creates new
> variables we have no problem the first time we run the syntax"), and
> were looking for syntax that would create these variables if
> necessary, and define them as a vector whether newly created or not.
> That was the problem I was addressing.
>
> Tell us more about what you're trying to do: create a contiguous set
> of variables, once? Create a contiguous set of variables, and then
> extend the set on successive runs? Or, what else?
>
> -Best wishes,
> Richard Ristow
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD