Date: Mon, 28 Jan 2008 11:57:48 -0600
Reply-To: Mark Palmberg <mark-palmberg@uiowa.edu>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Mark Palmberg <mark-palmberg@uiowa.edu>
Subject: Re: Syntax to transform numeric variable to date variable (x120)
In-Reply-To: <E0428445172CB7469BF187FF4D005149079AE790@CHIEMAIL1.spss.com>
Content-Type: text/plain; charset="us-ascii"
Does this syntax care that my date values aren't consecutive in my file,
Richard? Might that be a reason for the
*>Error # 4511 in column 256. Text: (End of Command) *>The number of
elements in variable and value lists do not agree in number *>with a
previous variable or value list(s).
error?
Mark
-----Original Message-----
From: Oliver, Richard [mailto:roliver@spss.com]
Sent: Friday, January 25, 2008 4:28 PM
To: Mark Palmberg; SPSSX-L@LISTSERV.UGA.EDU
Subject: RE: Syntax to transform numeric variable to date variable
(x120)
You can't use existing variable names as stand-in variable names, and
for this example you need one variable list that specifies the original
variables and one list that specifies the new variables being created,
as in:
data list free /date1 date2 date3.
begin data
20071028 20081029 20091027
end data.
do repeat oldvar=date1 to date3 /newvar=newdate1 to newdate3.
compute #day=mod(oldvar,100).
compute #year=trunc(oldvar/10000).
compute #month=trunc(mod(oldvar,10000)/100).
compute newvar=date.mdy(#month, #day, #year).
end repeat.
formats newdate1 to newdate3 (adate10).
list.
Note also that there was a typo in my previous response:
"compute newdate=" should have been "compute newvar="
since the declared name of the stand-in variable is newvar, not newdate.
"oldvar" and "newvar" are used as "stand-in" variables for the two
variable lists. You can use END REPEAT PRINT to see how the DO REPEAT
structure gets expanded:
data list free /date1 date2 date3.
begin data
20071028 20081029 20091027
end data.
do repeat oldvar=date1 to date3 /newvar=newdate1 to newdate3.
compute #day=mod(oldvar,100).
compute #year=trunc(oldvar/10000).
compute #month=trunc(mod(oldvar,10000)/100).
compute newvar=date.mdy(#month, #day, #year).
end repeat print.
51 0 +compute #day=mod(date1,100)
52 0 +compute #year=trunc(date1/10000)
53 0 +compute #month=trunc(mod(date1,10000)/100)
54 0 +compute newdate1=date.mdy(#month, #day, #year)
55 0 +compute #day=mod(date2,100)
56 0 +compute #year=trunc(date2/10000)
57 0 +compute #month=trunc(mod(date2,10000)/100)
58 0 +compute newdate2=date.mdy(#month, #day, #year)
59 0 +compute #day=mod(date3,100)
60 0 +compute #year=trunc(date3/10000)
61 0 +compute #month=trunc(mod(date3,10000)/100)
62 0 +compute newdate3=date.mdy(#month, #day, #year)
-----Original Message-----
From: Mark Palmberg [mailto:mark-palmberg@uiowa.edu]
Sent: Friday, January 25, 2008 4:02 PM
To: Oliver, Richard; SPSSX-L@LISTSERV.UGA.EDU
Subject: RE: Syntax to transform numeric variable to date variable
(x120)
How does the naming work, then, Richard? For example (using variables
from my file):
do repeat DATE0001=DATE0001 TO DATE0120 /GFTDT001=GFTDT001 TO GFTDT120.
compute #day=mod(DATE0001,100).
compute #year=trunc(DATE0001/10000).
compute #month=trunc(mod(DATE0001,10000)/100).
compute GFTDT001=date.mdy(#month, #day, #year).
end repeat.
formats GFTDT001 TO GFTDT120 (adate10).
Results in:
do repeat DATE0001=DATE0001 TO DATE0120 /GFTDT001=GFTDT001 TO GFTDT120.
*>Error # 4511 in column 256. Text: (End of Command) *>The number of
elements in variable and value lists do not agree in number *>with a
previous variable or value list(s).
compute #day=mod(DATE0001,100).
compute #year=trunc(DATE0001/10000).
compute #month=trunc(mod(DATE0001,10000)/100).
compute GFTDT001=date.mdy(#month, #day, #year).
end repeat.
*>Error # 4001. Command name: end repeat *>An END REPEAT command has
appeared without a previous DO REPEAT command.
formats GFTDT001 TO GFTDT120 (adate10).
*>Error # 4826 in column 21. Text: GFTDT120 *>An invalid variable list
was found on the FORMATS, PRINT FORMATS, or WRITE *>FORMATS command.
Note that these commands cannot be used to alter the *>formats of system
($) variables.
EXECUTE.
*>Warning # 613
*>One of the arguments to the DATE function is out of range or is not an
*>integer. The result has been set to the system-missing value.
-----Original Message-----
From: Oliver, Richard [mailto:roliver@spss.com]
Sent: Friday, January 25, 2008 3:35 PM
To: Mark Palmberg; SPSSX-L@LISTSERV.UGA.EDU
Subject: RE: Syntax to transform numeric variable to date variable
(x120)
If you need to do this for multiple date variables, use a DO REPEAT
structure.
do repeat oldvar=old varlist /newvar=new varlist.
compute #day=mod(oldvar,100).
compute #year=trunc(oldvar/10000).
compute #month=trunc(mod(oldvar,10000)/100).
compute newdate=date.mdy(#month, #day, #year).
end repeat.
formats new varlist (adate10).
-----Original Message-----
From: Mark Palmberg [mailto:mark-palmberg@uiowa.edu]
Sent: Friday, January 25, 2008 3:15 PM
To: Oliver, Richard; SPSSX-L@LISTSERV.UGA.EDU
Subject: RE: Syntax to transform numeric variable to date variable
(x120)
Syntax is really more art than science, isn't it?
This worked a treat, Richard. Thanks to Melissa I., too, who came up
with a nested version. Running through this will also solve my problem
of getting all my dates arranged contiguously in my file, it looks like,
which is a bonus.
Many, many thanks.
Mark
-----Original Message-----
From: Oliver, Richard [mailto:roliver@spss.com]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; SPSSX-L@LISTSERV.UGA.EDU
Subject: RE: Syntax to transform numeric variable to date variable
(x120)
The wizard will generate syntax that you can re-use, although in your
case it may not be the optimal choice. How about this:
compute #day=mod(datevar,100).
compute #year=trunc(datevar/10000).
compute #month=trunc(mod(datevar,10000)/100).
compute newdate=date.mdy(#month, #day, #year).
formats newdate (adate10).
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Syntax to transform numeric variable to date variable (x120)
The date variables my file came off the database as a 12-character
numeric field in the format yyyymmdd. I'd like to make these all date
variables with format mm/dd/yyyy. I'm finding that changing each date
variable to an 8-character string and then using the date/time wizard to
convert them is probably not the optimal way to accomplish this, and my
understanding is that the date/time wizard doesn't produce any syntax
for me to suss out? As previously mentioned, the file's variables are
arranged thus:
ID DATE0001 GFCRAM0001 DATE0002 GFCRAM0002...DATE0120 GFCRAM0120
Thanks so much for any thoughts you have. I'm using SPSS15, if it
matters.
Mark
=======
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (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
LISTSERV@LISTSERV.UGA.EDU (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