Syntax to transform numeric variable to date variable (x120)

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

Syntax to transform numeric variable to date variable (x120)

Mark Palmberg-2
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
[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: Syntax to transform numeric variable to date variable (x120)

Melissa Ives
compute
newdate1=date.mdy(trunc(mod(date0001,10000)/100),mod(date0001,100),trunc
(date0001/10000)).
format newdate1 (adate10).

Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
Subject: [SPSSX-L] 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
[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


PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.

=====================
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: Syntax to transform numeric variable to date variable (x120)

Oliver, Richard
In reply to this post by Mark Palmberg-2
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:[hidden email]] On Behalf Of Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax to transform numeric variable to date variable (x120)

Mark Palmberg-2
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:[hidden email]]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax to transform numeric variable to date variable (x120)

Oliver, Richard
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:[hidden email]]
Sent: Friday, January 25, 2008 3:15 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax to transform numeric variable to date variable (x120)

Mark Palmberg-2
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:[hidden email]]
Sent: Friday, January 25, 2008 3:35 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:15 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax to transform numeric variable to date variable (x120)

Oliver, Richard
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:[hidden email]]
Sent: Friday, January 25, 2008 4:02 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:35 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:15 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax to transform numeric variable to date variable (x120)

msherman
In reply to this post by Mark Palmberg-2
Mark: I think you rename DATE0001 to something like DATEX0001.

so it would look like
do repeat DATEEX0001=DATE0001 to DATE0120 / etc.

martin


>>> Mark Palmberg <[hidden email]> 1/25/2008 5:02 PM >>>
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:[hidden email]]
Sent: Friday, January 25, 2008 3:35 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:15 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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

=====================
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: Syntax to transform numeric variable to date variable (x120)

Mark Palmberg-2
In reply to this post by Oliver, Richard
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:[hidden email]]
Sent: Friday, January 25, 2008 4:28 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 4:02 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:35 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:15 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax to transform numeric variable to date variable (x120)

Oliver, Richard
From the help for DO REPEAT:

"All replacement variable and value lists must have the same number of items."

In other words:

do repeat x=var1 var2 var3 /y=newvar1 newvar2

will generate an error because the two variable lists don't have the same number of items.

Also, when referring to existing variables, the keyword TO refers to a sequence of variables in file order, but for new variables it must provide a meaningful way to expand to new variable names. So:

do repeat x=oldvar1 to oldfred /y=newvar1 to newvar5

is perfectly valid as long as oldfred comes after oldvar1 and the implied list of old variables contains five variables, but:

do repeat x=oldvar1 to oldfred /y=newvar1 to var5new

will generate an error because the implied list of new variables cannot be expanded.



-----Original Message-----
From: Mark Palmberg [mailto:[hidden email]]
Sent: Monday, January 28, 2008 11:58 AM
To: Oliver, Richard; [hidden email]
Subject: RE: Syntax to transform numeric variable to date variable (x120)

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:[hidden email]]
Sent: Friday, January 25, 2008 4:28 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 4:02 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:35 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 3:15 PM
To: Oliver, Richard; [hidden email]
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:[hidden email]]
Sent: Friday, January 25, 2008 2:42 PM
To: Mark Palmberg; [hidden email]
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:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Friday, January 25, 2008 1:31 PM
To: [hidden email]
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
[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
Reply | Threaded
Open this post in threaded view
|

Re: Syntax to transform numeric variable to date variable (x120)

Maguin, Eugene
In reply to this post by Mark Palmberg-2
Mark,

I'm not sure if this is the problem but didn't your original file have an
order of date1, gift1, date2, gift2, etc?

If so and if you did not change the order before the do repeat then that is
a problem because of the way that the 'TO' keyword works. If the variable
order in the file is
A1 d1 a2 d2 a3 d3 a4 d4, a1 to a4 includes d1 d2 d3 as well as a2 and a3.
There may be other problems but this will be a problem.

You can get around this problem by listing all of the variables to be used.

Gene Maguin

=====================
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: Syntax to transform numeric variable to date variable (x120)

Mark Palmberg-2
I think that's it, Gene.  I realize now that I phrased my last question
to the list poorly (read: downright incorrectly).  When I wrote that my
"date values weren't consecutive," what I meant was that the date
*variables* aren't consecutive in the file, as you correctly recall (and
as I specified way back in my original message, whenever that was).  It
would makes sense that SPSS is returning an error when it finds
GFCRAM0001 as the second value in a variable list that's supposed to be
all DATE0001 TO DATE0120.

Mark

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Gene Maguin
Sent: Monday, January 28, 2008 12:43 PM
To: [hidden email]
Subject: Re: Syntax to transform numeric variable to date variable
(x120)

Mark,

I'm not sure if this is the problem but didn't your original file have
an order of date1, gift1, date2, gift2, etc?

If so and if you did not change the order before the do repeat then that
is a problem because of the way that the 'TO' keyword works. If the
variable order in the file is
A1 d1 a2 d2 a3 d3 a4 d4, a1 to a4 includes d1 d2 d3 as well as a2 and
a3.
There may be other problems but this will be a problem.

You can get around this problem by listing all of the variables to be
used.

Gene Maguin

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

Re: Syntax to transform numeric variable to date variable (x120)

Mark Palmberg-2
In reply to this post by Maguin, Eugene
Gene,

I reorganized my file using KEEP (thanks for the help there) so that it
now looks like:

ID DATE0001 DATE0002 DATE0003...DATE0120 GFTAMT0001 GFTAMT0002
GFTAMT0003...GFTAMT0120

And then ran this syntax again:

do repeat oldvar=DATE0001 TO DATE0120 /newvar=DATE1 TO DATE120.
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 DATE1 TO DATE120 (adate10).

This syntax gave me the following output:

do repeat oldvar=DATE0001 TO DATE0120 /newvar=DATE1 TO DATE120.
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 DATE1 TO DATE120 (adate10).
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.

So you were right in assuming there are probably other issues involved
here.

Thanks!

Mark

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Gene Maguin
Sent: Monday, January 28, 2008 12:43 PM
To: [hidden email]
Subject: Re: Syntax to transform numeric variable to date variable
(x120)

Mark,

I'm not sure if this is the problem but didn't your original file have
an order of date1, gift1, date2, gift2, etc?

If so and if you did not change the order before the do repeat then that
is a problem because of the way that the 'TO' keyword works. If the
variable order in the file is
A1 d1 a2 d2 a3 d3 a4 d4, a1 to a4 includes d1 d2 d3 as well as a2 and
a3.
There may be other problems but this will be a problem.

You can get around this problem by listing all of the variables to be
used.

Gene Maguin

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

Re: Syntax to transform numeric variable to date variable (x120)

Oliver, Richard
Are all of your date values set to system-missing, or just some of them? This syntax will only work with numeric original values of the form yyyymmdd, where all values are exactly 8 digits, all years are 4 digits, all months are 2 digits from 01 to 12, and all days are 2 digits from 01 to 31 (with the month value determining the upper valid day value). In other words, this will work fine for 20070111, which will be converted to 01/11/2007 -- but it won't work for 2007111, which would be tricky to convert with any routine since 111 could be January 11 or November 1.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mark Palmberg
Sent: Monday, January 28, 2008 4:32 PM
To: [hidden email]
Subject: Re: Syntax to transform numeric variable to date variable (x120)

Gene,

I reorganized my file using KEEP (thanks for the help there) so that it
now looks like:

ID DATE0001 DATE0002 DATE0003...DATE0120 GFTAMT0001 GFTAMT0002
GFTAMT0003...GFTAMT0120

And then ran this syntax again:

do repeat oldvar=DATE0001 TO DATE0120 /newvar=DATE1 TO DATE120.
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 DATE1 TO DATE120 (adate10).

This syntax gave me the following output:

do repeat oldvar=DATE0001 TO DATE0120 /newvar=DATE1 TO DATE120.
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 DATE1 TO DATE120 (adate10).
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.

So you were right in assuming there are probably other issues involved
here.

Thanks!

Mark

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Gene Maguin
Sent: Monday, January 28, 2008 12:43 PM
To: [hidden email]
Subject: Re: Syntax to transform numeric variable to date variable
(x120)

Mark,

I'm not sure if this is the problem but didn't your original file have
an order of date1, gift1, date2, gift2, etc?

If so and if you did not change the order before the do repeat then that
is a problem because of the way that the 'TO' keyword works. If the
variable order in the file is
A1 d1 a2 d2 a3 d3 a4 d4, a1 to a4 includes d1 d2 d3 as well as a2 and
a3.
There may be other problems but this will be a problem.

You can get around this problem by listing all of the variables to be
used.

Gene Maguin

=====================
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

=====================
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: Syntax to transform numeric variable to date variable (x120)

Mark Palmberg-2
It actually looks like it worked great, Richard.  *All* of the original
numeric values were in the format yyyymmdd, and a cursory audit of the
new values created by the syntax below reveals that everything's where
it's supposed to be.

I'm very thankful to you, Melissa, Martin, and Gene.  Thanks also to the
list for enduring all my traffic yesterday.

Mark

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Oliver, Richard
Sent: Monday, January 28, 2008 7:46 PM
To: [hidden email]
Subject: Re: Syntax to transform numeric variable to date variable
(x120)

Are all of your date values set to system-missing, or just some of them?
This syntax will only work with numeric original values of the form
yyyymmdd, where all values are exactly 8 digits, all years are 4 digits,
all months are 2 digits from 01 to 12, and all days are 2 digits from 01
to 31 (with the month value determining the upper valid day value). In
other words, this will work fine for 20070111, which will be converted
to 01/11/2007 -- but it won't work for 2007111, which would be tricky to
convert with any routine since 111 could be January 11 or November 1.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Mark Palmberg
Sent: Monday, January 28, 2008 4:32 PM
To: [hidden email]
Subject: Re: Syntax to transform numeric variable to date variable
(x120)

Gene,

I reorganized my file using KEEP (thanks for the help there) so that it
now looks like:

ID DATE0001 DATE0002 DATE0003...DATE0120 GFTAMT0001 GFTAMT0002
GFTAMT0003...GFTAMT0120

And then ran this syntax again:

do repeat oldvar=DATE0001 TO DATE0120 /newvar=DATE1 TO DATE120.
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 DATE1 TO DATE120 (adate10).

This syntax gave me the following output:

do repeat oldvar=DATE0001 TO DATE0120 /newvar=DATE1 TO DATE120.
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 DATE1 TO DATE120 (adate10).
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.

So you were right in assuming there are probably other issues involved
here.

Thanks!

Mark

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Gene Maguin
Sent: Monday, January 28, 2008 12:43 PM
To: [hidden email]
Subject: Re: Syntax to transform numeric variable to date variable
(x120)

Mark,

I'm not sure if this is the problem but didn't your original file have
an order of date1, gift1, date2, gift2, etc?

If so and if you did not change the order before the do repeat then that
is a problem because of the way that the 'TO' keyword works. If the
variable order in the file is
A1 d1 a2 d2 a3 d3 a4 d4, a1 to a4 includes d1 d2 d3 as well as a2 and
a3.
There may be other problems but this will be a problem.

You can get around this problem by listing all of the variables to be
used.

Gene Maguin

=====================
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

=====================
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