STRING TO DATE?

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

STRING TO DATE?

Raffe, Sydelle, SSA
I have string vars in the format 08/31/2007. How can I use syntax to convert them to date vars? Thanks.

Sydelle Raffe, Alameda County Social Services Agency
Information Services Division, Office of Data Management
e:mail:  [hidden email]
phone: 510-271-9174     fax: 510-271-9107
If you have a request for information, please submit an ODM request form at:  https://alamedasocialservices.org/staff/support_services/statistics_and_reports/odm/index.cfm

====================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: STRING TO DATE?

ariel barak
Sydelle,

The text below is copied from Raynald's SPSS Tools website -

http://www.spsstools.net/LearningSyntax.htm#DateTutorial

HTH,
Ariel



** 3. Convert a string containing a date into a date variable.*

DATA LIST LIST /datestr(A10).
BEGIN DATA
11/26/1966
01/15/1981
END DATA.
LIST. *

--> method 1* (a general method)

COMPUTE mth=NUMBER(SUBSTR(datestr,1,2),F8.0).
COMPUTE day=NUMBER(SUBSTR(datestr,4,2),F8.0).
COMPUTE yr=NUMBER(SUBSTR(datestr,7),F8.0).
COMPUTE mydate=DATE.DMY(day,mth,yr).
FORMAT mydate(SDATE11).
VARIABLE WIDTH mydate (11).
EXECUTE.

* The date in the above string variable has the form mm/dd/yyyy. The code
works as
* is if the initial format is mm.dd.yyyy or mm-dd-yyyy. It is easy to modify
the above
* to handle variations such as yyyy/mm/dd, dd/mm/yyyy. *

--> method 2* (works only when data fit an existing SPSS date format)

COMPUTE mydate=NUMBER(datestr,ADATE10).
FORMATS mydate(ADATE10).
VARIABLE WIDTH mydate(10).  /* The purpose of this line is to display all 4
digits of the year in the data editor */.
EXECUTE.


On Dec 20, 2007 12:33 PM, Raffe, Sydelle, SSA <[hidden email]> wrote:

> I have string vars in the format 08/31/2007. How can I use syntax to
> convert them to date vars? Thanks.
>
> Sydelle Raffe, Alameda County Social Services Agency
> Information Services Division, Office of Data Management
> e:mail:  [hidden email]
> phone: 510-271-9174     fax: 510-271-9107
> If you have a request for information, please submit an ODM request form
> at:
> https://alamedasocialservices.org/staff/support_services/statistics_and_reports/odm/index.cfm
>
> 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: STRING TO DATE?

Oliver, Richard
In this case, I think only the first two lines of method 2 are needed:
 
COMPUTE mydate=NUMBER(datestr,ADATE10).
FORMATS mydate(ADATE10).


________________________________

From: SPSSX(r) Discussion on behalf of Ariel Barak
Sent: Thu 12/20/2007 1:05 PM
To: [hidden email]
Subject: Re: STRING TO DATE?



Sydelle,

The text below is copied from Raynald's SPSS Tools website -

http://www.spsstools.net/LearningSyntax.htm#DateTutorial

HTH,
Ariel



** 3. Convert a string containing a date into a date variable.*

DATA LIST LIST /datestr(A10).
BEGIN DATA
11/26/1966
01/15/1981
END DATA.
LIST. *

--> method 1* (a general method)

COMPUTE mth=NUMBER(SUBSTR(datestr,1,2),F8.0).
COMPUTE day=NUMBER(SUBSTR(datestr,4,2),F8.0).
COMPUTE yr=NUMBER(SUBSTR(datestr,7),F8.0).
COMPUTE mydate=DATE.DMY(day,mth,yr).
FORMAT mydate(SDATE11).
VARIABLE WIDTH mydate (11).
EXECUTE.

* The date in the above string variable has the form mm/dd/yyyy. The code
works as
* is if the initial format is mm.dd.yyyy or mm-dd-yyyy. It is easy to modify
the above
* to handle variations such as yyyy/mm/dd, dd/mm/yyyy. *

--> method 2* (works only when data fit an existing SPSS date format)

COMPUTE mydate=NUMBER(datestr,ADATE10).
FORMATS mydate(ADATE10).
VARIABLE WIDTH mydate(10).  /* The purpose of this line is to display all 4
digits of the year in the data editor */.
EXECUTE.


On Dec 20, 2007 12:33 PM, Raffe, Sydelle, SSA <[hidden email]> wrote:

> I have string vars in the format 08/31/2007. How can I use syntax to
> convert them to date vars? Thanks.
>
> Sydelle Raffe, Alameda County Social Services Agency
> Information Services Division, Office of Data Management
> e:mail:  [hidden email]
> phone: 510-271-9174     fax: 510-271-9107
> If you have a request for information, please submit an ODM request form
> at:
> https://alamedasocialservices.org/staff/support_services/statistics_and_reports/odm/index.cfm
>
> 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: STRING TO DATE?

ViAnn Beadle
In reply to this post by Raffe, Sydelle, SSA
Use the Date/Time wizard from the transform menu and select radio button 2.
At the end, paste the syntax to see how it works.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Raffe, Sydelle, SSA
Sent: Thursday, December 20, 2007 11:33 AM
To: [hidden email]
Subject: STRING TO DATE?

I have string vars in the format 08/31/2007. How can I use syntax to convert
them to date vars? Thanks.

Sydelle Raffe, Alameda County Social Services Agency
Information Services Division, Office of Data Management
e:mail:  [hidden email]
phone: 510-271-9174     fax: 510-271-9107
If you have a request for information, please submit an ODM request form at:
https://alamedasocialservices.org/staff/support_services/statistics_and_repo
rts/odm/index.cfm

=======
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: STRING TO DATE?

Raffe, Sydelle, SSA
Wow -- never even saw that. Works great!! Thank you.

-----Original Message-----
From: ViAnn Beadle [mailto:[hidden email]]
Sent: Thursday, December 20, 2007 1:09 PM
To: Raffe, Sydelle, SSA; [hidden email]
Subject: RE: STRING TO DATE?


Use the Date/Time wizard from the transform menu and select radio button 2.
At the end, paste the syntax to see how it works.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Raffe, Sydelle, SSA
Sent: Thursday, December 20, 2007 11:33 AM
To: [hidden email]
Subject: STRING TO DATE?

I have string vars in the format 08/31/2007. How can I use syntax to convert
them to date vars? Thanks.

Sydelle Raffe, Alameda County Social Services Agency
Information Services Division, Office of Data Management
e:mail:  [hidden email]
phone: 510-271-9174     fax: 510-271-9107
If you have a request for information, please submit an ODM request form at:
https://alamedasocialservices.org/staff/support_services/statistics_and_repo
rts/odm/index.cfm

=======
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: STRING TO DATE?

Raffe, Sydelle, SSA
In reply to this post by Oliver, Richard
Yes, I was hoping there was something cleaner than deconstructing and reconstructing the dates. Thanks.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of
Oliver, Richard
Sent: Thursday, December 20, 2007 11:27 AM
To: [hidden email]
Subject: Re: STRING TO DATE?


In this case, I think only the first two lines of method 2 are needed:

COMPUTE mydate=NUMBER(datestr,ADATE10).
FORMATS mydate(ADATE10).


________________________________

From: SPSSX(r) Discussion on behalf of Ariel Barak
Sent: Thu 12/20/2007 1:05 PM
To: [hidden email]
Subject: Re: STRING TO DATE?



Sydelle,

The text below is copied from Raynald's SPSS Tools website -

http://www.spsstools.net/LearningSyntax.htm#DateTutorial

HTH,
Ariel



** 3. Convert a string containing a date into a date variable.*

DATA LIST LIST /datestr(A10).
BEGIN DATA
11/26/1966
01/15/1981
END DATA.
LIST. *

--> method 1* (a general method)

COMPUTE mth=NUMBER(SUBSTR(datestr,1,2),F8.0).
COMPUTE day=NUMBER(SUBSTR(datestr,4,2),F8.0).
COMPUTE yr=NUMBER(SUBSTR(datestr,7),F8.0).
COMPUTE mydate=DATE.DMY(day,mth,yr).
FORMAT mydate(SDATE11).
VARIABLE WIDTH mydate (11).
EXECUTE.

* The date in the above string variable has the form mm/dd/yyyy. The code
works as
* is if the initial format is mm.dd.yyyy or mm-dd-yyyy. It is easy to modify
the above
* to handle variations such as yyyy/mm/dd, dd/mm/yyyy. *

--> method 2* (works only when data fit an existing SPSS date format)

COMPUTE mydate=NUMBER(datestr,ADATE10).
FORMATS mydate(ADATE10).
VARIABLE WIDTH mydate(10).  /* The purpose of this line is to display all 4
digits of the year in the data editor */.
EXECUTE.


On Dec 20, 2007 12:33 PM, Raffe, Sydelle, SSA <[hidden email]> wrote:

> I have string vars in the format 08/31/2007. How can I use syntax to
> convert them to date vars? Thanks.
>
> Sydelle Raffe, Alameda County Social Services Agency
> Information Services Division, Office of Data Management
> e:mail:  [hidden email]
> phone: 510-271-9174     fax: 510-271-9107
> If you have a request for information, please submit an ODM request form
> at:
> https://alamedasocialservices.org/staff/support_services/statistics_and_reports/odm/index.cfm
>
> 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