Date: Mon, 17 Oct 2005 16:28:41 -0400
Reply-To: Richard Ristow <wrristow@mindspring.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Richard Ristow <wrristow@mindspring.com>
Subject: Re: Determining work schedules
In-Reply-To: <5afd81020510151651w51bcf794o3cdb00750fe23f37@mail.gmail.co m>
Content-Type: text/plain; charset="us-ascii"; format=flowed
At 07:51 PM 10/15/2005, Vincent Louis wrote:
> I am working thru a problem I hope some of you may be able to help
> me with.
> I have a dataset which contains the following variables: (1) hours
> wives and husbands start work each day; (2) hours wives and husbands
> finish work each day. I am interested in determining the work
> schedules of each wife and each husband. I want to have the following
> work shifts:
> (1) Fixed day shift (if at least half of work day falls betwn 8 am
> and 4 pm);
> (2) Fixed evening shift (if at least half of work day falls betwn 4
> pm and 12 midnight);
> (3) Fixed night shift (if at least half of work day falls betwn 12
> midnight and 8 am).
> I would like to place each husband and each wife in one of these
> work shifts. I would also like to determine by how many hours the
> work shifts of wives and husbands overlap.
> Can anyone help me to write a spss syntax to work out the above?
"Blessed are they who post with test data, for they shall receive
answers." Here's a case very much in point. It's quite difficult to
generate reasonable test data. (IF you post, please limit data and
header lines to 72 characters. See earlier post.)
It sounds as if you have multiple records for each couple, probably
denoting several work days, or you couldn't talk about "fixed." But
there's no dealing with that, without more information.
Try this. It's for one person only; duplicate it, changing variable
names, for the two spouses. Use a DO REPEAT if you like; that makes it
easier to correct the logic, if you need to.
Variables:
H_STRTWK 'Time husband starts work'
H_ENDSWK 'Time husband ends work'.
Assume: Both these are SPSS time variables, with values between 0 and
TIME.HMS(24). (That's an SPSS time function. I'm referring to times by
function calls, rather than "magic number" constants.)
It uses scratch variables extensively to avoid cluttering the output
file with intermediate values. For debugging, an easy course is to
change '#' to '@' throughout, and make them regular variables.
Of course, THIS CODE IS NOT TESTED, and it has unusually rich
opportunities for errors. It is far from the most compact code
possible, and probably not the most elegant. (Jan Spousta, here's your
opportunity.)
* Correct for an end time in the next day .
. COMPUTE #CORREND = H_ENDSWK.
. IF (H_ENDSWK LT H_STRTWK)
#CORREND = H_ENDSWK + TIME.HMS(24).
* Pseudo-constants: Shift start & end times .
. COMPUTE #STT_DAY = TIME.HMS(8).
. COMPUTE #END_DAY = TIME.HMS(16) /*04_12*/.
. COMPUTE #STT_EVE = TIME.HMS(16) /*04+12*/.
. COMPUTE #END_EVE = TIME.HMS(24).
. COMPUTE #STT_NT = TIME.HMS(24).
. COMPUTE #END_NT = TIME.HMS(32) /*08+24*/.
* How much time in each shift? .
. DO REPEAT
SHFT_ST = #STT_DAY #STT_EVE #STT_NT
/SHFT_END= #END_DAY #END_EVE #END_NT
/WORK_ST = #DAYSTRT #EVESTRT #NGTSTRT
/WORD_END= #DAYEND #EVEEND #NGTEND
/SHFT_WRK= #DAYWORK #EVEWORK #NGTWORK.
* Starting point of work in this shift .
- DO IF H_STRTWK LT SHFT_ST.
. COMPUTE WORK_ST = SHFT_ST.
- ELSE IF H_STRTWK GT SHFT_END.
. COMPUTE WORK_ST = SHFT_END.
- ELSE.
. COMPUTE WORK_ST = H_STRTWK.
- END IF.
* Ending point of work in this shift .
- DO IF #CORREND LT SHFT_ST.
. COMPUTE WORK_END = SHFT_ST.
- ELSE IF #CORREND GT SHFT_END.
. COMPUTE WORK_END = SHFT_END.
- ELSE.
. COMPUTE WORK_END = H_STRTWK.
- END IF.
* Time worked in this shift .
* (Recall: the difference of two valid .
* time values is a valid time value.) .
. COMPUTE SHFT_WRK = WORK_END = WORK_ST.
. END REPEAT.
* Total length of the work day .
. COMPUTE #WRKDAY = #CORREND - HSTRTWK.
* Here's the variable you want .
. NUMERIC H_SHIFT (F2).
. VAR LABEL H_SHIFT
"Assignment of husband's work day to shift".
. VAL LABEL H_SHFT
1 'Day'
2 'Evening'
3 'Night'
8 'Other'
9 'Not calculable'.
. MISSING VALUE H_SHFT (9).
/SHFT_WRK= #DAYWORK #EVEWORK #NGTWORK.
. DO IF #DAYWORK/#WRKDAY GE 0.5.
. COMPUTE H_SHFT = 1.
. ELSE IF #EVEWORK/#WRKDAY GE 0.5.
. COMPUTE H_SHFT = 2.
. ELSE IF #NGTWORK/#WRKDAY GE 0.5.
. COMPUTE H_SHFT = 3.
. ELSE.
. COMPUTE H_SHFT = 8.
. END IF.
. RECODE H_SHFT (MISSING=9).