Date: Tue, 12 Oct 2004 20:29:30 -0500
Reply-To: "Marks, Jim" <Jim.Marks@lodgenet.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Marks, Jim" <Jim.Marks@lodgenet.com>
Subject: Re: editing syntax so a date can be split into day,
month and year columns
Content-Type: text/plain; charset="us-ascii"
Bob:
Do the date parts have to be strings? You can use numbers to produce
date variables in SPSS (e.g. COMPUTE date eq Date.dmy(day,month,year).)
This produces both number and string variables:
** data in day month year.
DATA LIST LIST /datestr (a10).
BEGIN DATA
11.07.1962
1.7.66
4.12.2000
END DATA.
** substring points for easy computes.
COMPUTE dot1 EQ index(datestr,'.').
COMPUTE dot2 EQ index(substr(datestr,dot1+1),'.').
** numbers.
COMPUTE day EQ NUMBER(substr(datestr,1,dot1),F8.0).
COMPUTE month EQ NUMBER(substr(datestr,dot1+1,dot2),f8).
COMPUTE year eq NUMBER(substr(datestr,dot2+dot1+1),f8).
** strings.
STRING da mo (a2) /yr (a4).
DO IF dot1 eq 3.
COMP da eq substr(datestr,1,dot1).
ELSE.
COMP da EQ concat('0',substr(datestr,1,dot1)).
END IF.
DO IF dot2 eq 3.
COMP mo eq substr(datestr,dot1+1,dot2).
ELSE.
COMP mo EQ concat('0',substr(datestr,dot1+1,dot1)).
END IF.
** I assume all 2 digit years are 19xx.
DO IF LEN(rtrim(substr(datestr,dot2+dot1+1))) GT 2.
COMP yr eq substr(datestr,dot2+dot1+1).
ELSE.
COMP yr eq concat('19',substr(datestr,dot2+dot1+1,2)).
END IF.
exe.
--jim
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU]On Behalf Of
Bob Green
Sent: Tuesday, October 12, 2004 3:16 PM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: editing syntax so a date can be split into day, month and year
columns
I located syntax (below) written by Mark Casazza which performs the task
of
splitting a date recorded as 11071962 into the respective day, month and
year columns. I am helping a colleague with their data which is in
several
different formats. 11/07/62 is easily edited, however other sections of
the
data are problematic because the above date is recorded as 11.7.62.
Removing the decimals results in 11762, and if the date had been 1.7.62,
the result would be 1762. Any assistance regarding syntax to add a '0'
in
all instances where there was only 1 value between the first and second
decimals is appreciated.
regards
Bob Green
--
*sample data.
data list fixed /strdate 1-8 (a).
begin data
11071962
end data.
list.
string mo da (a2)/ yr (a4).
compute mo = substr(strdate,1,2).
compute da = substr(strdate,3,2).
compute yr = substr(strdate,5,4).
list.
--
|