Date: Fri, 19 Sep 2003 17:03:17 -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: Creating more variables based on Date variable (Help needed
again) Thanks!
Content-Type: text/plain; charset="us-ascii"; format=flowed
CORRECTED POSTING:
A. Fix a bug in calculation of last day of the month. It is now one day
before the first day of the next month, rather than one second before.
The bug added one day to intervals whose endpoint was in a later month.
B. Add five test cases to show that the code can handle starting and
ending dates outside the current year (2003).
C. Change listing format for month start and end dates to DATETIME;
this was used to catch bug A.
At 03:56 PM 9/19/2003 +0800, Tans Family wrote:
>Below is the sample syntax and table which I hope to produce. The
>below syntax will produce the count for each month. The problem I am
>facing now is to replace the "count" in each month with the the "no of
>days" (based on the stdate and eddate).
I see that I didn't give you quite what you wanted, but interchanged
row and columns. However, it does give the substantive information; see
how you like it, as a start.
This is completely new syntax, not a modification of what you posted.
It is tested; this is SPSS draft output from the run.
* Data, from original posting .
* (ID values 50 and above added as test data) .
data list list/id (f2.0) stdate (adate9) eddate (adate9).
begin data.
01 08/30/03 08/30/03
11 09/16/03 09/17/03
16 09/17/03 09/17/03
18 09/17/03 09/19/03
19 08/20/03 10/20/03
50 12/15/02 01/10/03
51 12/15/03 01/10/04
52 11/15/02 02/02/04
53 10/20/02 03/15/03
54 10/20/03 03/15/04
end data.
/* Vectors: Months in current year, and first and last day of */
/* each of the months */
VECTOR MONTH(12,F2).
VECTOR #Fst(12).
VECTOR #Lst(12).
DO IF ($CASENUM = 1).
. COMPUTE #THIS_YR = XDATE.YEAR($TIME).
. LOOP #Mon_Num = 1 TO 12.
. COMPUTE #Fst(#Mon_Num)
= DATE.DMY(1,#Mon_Num,#THIS_YR).
. END LOOP.
. LOOP #Mon_Num = 1 TO 11.
. COMPUTE #Lst(#Mon_Num)
= #Fst(#Mon_Num+1) - TIME.DAYS(1).
. END LOOP.
. COMPUTE #Lst(12)
= DATE.DMY(1,1,#THIS_YR+1) - TIME.DAYS(1).
/* Test: Print beginning and end dates of each month in this year */
. PRINT / 'TEST: start and end dates of months this year' 03.
. PRINT / 'Month' 03
'Start' 10
'End' 33.
. LOOP #Mon_Num = 1 TO 12.
. COMPUTE #Fst_Dy = #Fst(#Mon_Num).
. COMPUTE #Lst_Dy = #Lst(#Mon_Num).
. PRINT / #Mon_Num 03-04 (F)
#Fst_Dy 10-30 (DATETIME)
#Lst_Dy 33-53 (DATETIME).
. END LOOP.
. PRINT /' '.
END IF.
LOOP #Mon_Num = 1 TO 12.
. DO IF (EDDATE < #Fst(#Mon_Num)).
. COMPUTE MONTH(#Mon_Num) = 0.
. ELSE IF (STDATE > #Lst(#Mon_Num)).
. COMPUTE MONTH(#Mon_Num) = 0.
. ELSE.
/* Compute earliest & latest day THIS MONTH */
/* within the interval for this record */
. COMPUTE #Fst_Dy = MAX(STDATE,#Fst(#Mon_Num)).
. COMPUTE #Lst_Dy = MIN(EDDATE,#Lst(#Mon_Num)).
. COMPUTE Month(#Mon_Num)
= 1 + CTIME.DAYS(#Lst_Dy - #Fst_Dy).
* *** DEBUGGING CODE *** .
*--- COMPUTE #Days = Month(#Mon_Num).
*--- PRINT /
ID 03-05 (F)
STDATE 08-17 (ADATE)
EDDATE 20-29 (ADATE)
#Mon_Num 32-34 (F)
':'
#Days 36-39 (F)
#Fst_Dy 42-51 (ADATE)
'-'
#Lst_Dy 53-64 (ADATE).
. END IF.
END LOOP.
EXECUTE.
TEST: start and end dates of months this year
Month Start End
1 01-JAN-2003 00:00:00 31-JAN-2003 00:00:00
2 01-FEB-2003 00:00:00 28-FEB-2003 00:00:00
3 01-MAR-2003 00:00:00 31-MAR-2003 00:00:00
4 01-APR-2003 00:00:00 30-APR-2003 00:00:00
5 01-MAY-2003 00:00:00 31-MAY-2003 00:00:00
6 01-JUN-2003 00:00:00 30-JUN-2003 00:00:00
7 01-JUL-2003 00:00:00 31-JUL-2003 00:00:00
8 01-AUG-2003 00:00:00 31-AUG-2003 00:00:00
9 01-SEP-2003 00:00:00 30-SEP-2003 00:00:00
10 01-OCT-2003 00:00:00 31-OCT-2003 00:00:00
11 01-NOV-2003 00:00:00 30-NOV-2003 00:00:00
12 01-DEC-2003 00:00:00 31-DEC-2003 00:00:00
LIST.
List
19 Sep 03
M M M M M M M M
O O O O O O O O
N N N N N N N N
T T T T T T T T
H H H H H H H H
ID STDATE EDDATE 1 2 3 4 5 6 7 8 MONTH9 MONTH10 MONTH11 MONTH12
1 08/30/03 08/30/03 0 0 0 0 0 0 0 1 0 0 0 0
11 09/16/03 09/17/03 0 0 0 0 0 0 0 0 2 0 0 0
16 09/17/03 09/17/03 0 0 0 0 0 0 0 0 1 0 0 0
18 09/17/03 09/19/03 0 0 0 0 0 0 0 0 3 0 0 0
19 08/20/03 10/20/03 0 0 0 0 0 0 0 12 30 20 0 0
50 12/15/02 01/10/03 10 0 0 0 0 0 0 0 0 0 0 0
51 12/15/03 01/10/04 0 0 0 0 0 0 0 0 0 0 0 17
52 11/15/02 02/02/04 31 28 31 30 31 30 31 31 30 31 30 31
53 10/20/02 03/15/03 31 28 15 0 0 0 0 0 0 0 0 0
54 10/20/03 03/15/04 0 0 0 0 0 0 0 0 0 12 30 31
Number of cases read: 10 Number of cases listed: 10