How can I convert a date to a string version of itself?

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

How can I convert a date to a string version of itself?

Martha Hewett
I have a variable for site ID, and another variable for date.  Each site and date then has a bunch of records associated with it.  I want to create a variable that will be an identifier for each site visit, e.g. a particular value of that variable might be 1027_Feb_11_2009 for site 1027, the visit of Feb 11, 2009.  This is probably obvious but my attempts have not been working.


Martha Hewett
Director of Research | 612.335.5865
Center for Energy and Environment
212 Third Avenue North, Suite 560 | Minneapolis, MN 55401


Reply | Threaded
Open this post in threaded view
|

Re: How can I convert a date to a string version of itself?

Rick Oliver-3
So you have numeric variable that contains a site number and data variable that contains the visit date, and you want to concatenate them into a single string?

Rick Oliver
Senior Information Developer
Business Analytics (SPSS)
E-mail: [hidden email]





From:        Martha Hewett <[hidden email]>
To:        [hidden email]
Date:        11/22/2011 03:34 PM
Subject:        How can I convert a date to a string version of itself?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I have a variable for site ID, and another variable for date.  Each site and date then has a bunch of records associated with it.  I want to create a variable that will be an identifier for each site visit, e.g. a particular value of that variable might be 1027_Feb_11_2009 for site 1027, the visit of Feb 11, 2009.  This is probably obvious but my attempts have not been working.


Martha Hewett
Director of Research | 612.335.5865
Center for Energy and Environment
212 Third Avenue North, Suite 560 | Minneapolis, MN 55401



Reply | Threaded
Open this post in threaded view
|

Automatic reply: How can I convert a date to a string version of itself?

Kelly Vander Ley

I will be out of the office on Wednesday 11/23 through Friday 11/25, returning on Monday (11/28). If you need immediate assistance please call the main office number 503/223-8248 or 800/788-1887 and the receptionist will ensure that I get the message.  Happy Thanksgiving!  Kelly

 

 

Reply | Threaded
Open this post in threaded view
|

Re: How can I convert a date to a string version of itself?

David Marso
Administrator
In reply to this post by Martha Hewett
LOOK at the SUBSTR, CONCAT, STRING functions!

DATA LIST / ID DATE (F4,X,DATE11).
BEGIN DATA
1234 11-Feb-2009
END DATA.
LIST.

STRING NewID(A16).
STRING #TMP (A11).
COMPUTE #TMP=STRING(DATE,DATE11).
COMPUTE NewID=CONCAT(STRING(ID,N4),"_",SUBSTR(#TMP,4,3),"_",SUBSTR(#TMP,1,2),"_",SUBSTR(#TMP,8)).
LIST.

  ID        DATE NEWID

1234 11-FEB-2009 1234_FEB_11_2009



Martha Hewett wrote
I have a variable for site ID, and another variable for date.  Each site
and date then has a bunch of records associated with it.  I want to create
a variable that will be an identifier for each site visit, e.g. a
particular value of that variable might be 1027_Feb_11_2009 for site 1027,
the visit of Feb 11, 2009.  This is probably obvious but my attempts have
not been working.


Martha Hewett
Director of Research | 612.335.5865
Center for Energy and Environment
212 Third Avenue North, Suite 560 | Minneapolis, MN 55401
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: How can I convert a date to a string version of itself?

Bruce Weaver
Administrator
And if you were content with "1234-11-Feb-2009" rather than "1234_Feb_11_2009", the syntax could be made even simpler--see NewID3 below.  ;-)


DATA LIST / ID DATE (F4,X,DATE11).
BEGIN DATA
1234 11-Feb-2009
END DATA.
LIST.

STRING NewID1 to NewID3(A16).
STRING #TMP (A11).
COMPUTE #TMP=STRING(DATE,DATE11).
* NewID1 is DM's version.
COMPUTE NewID1 =
  CONCAT(STRING(ID,N4),"_",SUBSTR(#TMP,4,3),"_",SUBSTR(#TMP,1,2),"_",SUBSTR(#TMP,8)).
COMPUTE NewID2 = REPLACE(CONCAT(STRING(ID,N4),"_",#TMP),"-","_").
COMPUTE NewID3 = CONCAT(STRING(ID,N4),"-",#TMP).
LIST.


David Marso wrote
LOOK at the SUBSTR, CONCAT, STRING functions!

DATA LIST / ID DATE (F4,X,DATE11).
BEGIN DATA
1234 11-Feb-2009
END DATA.
LIST.

STRING NewID(A16).
STRING #TMP (A11).
COMPUTE #TMP=STRING(DATE,DATE11).
COMPUTE NewID=CONCAT(STRING(ID,N4),"_",SUBSTR(#TMP,4,3),"_",SUBSTR(#TMP,1,2),"_",SUBSTR(#TMP,8)).
LIST.

  ID        DATE NEWID

1234 11-FEB-2009 1234_FEB_11_2009



Martha Hewett wrote
I have a variable for site ID, and another variable for date.  Each site
and date then has a bunch of records associated with it.  I want to create
a variable that will be an identifier for each site visit, e.g. a
particular value of that variable might be 1027_Feb_11_2009 for site 1027,
the visit of Feb 11, 2009.  This is probably obvious but my attempts have
not been working.


Martha Hewett
Director of Research | 612.335.5865
Center for Energy and Environment
212 Third Avenue North, Suite 560 | Minneapolis, MN 55401
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: How can I convert a date to a string version of itself?

Martha Hewett
In reply to this post by Rick Oliver-3
Right.

Martha Hewett
Director of Research | 612.335.5865
Center for Energy and Environment
212 Third Avenue North, Suite 560 | Minneapolis, MN 55401






From:        Rick Oliver <[hidden email]>
To:        Martha Hewett <[hidden email]>
Cc:        [hidden email]
Date:        11/22/2011 03:56 PM
Subject:        Re: How can I convert a date to a string version of itself?




So you have numeric variable that contains a site number and data variable that contains the visit date, and you want to concatenate them into a single string?

Rick Oliver
Senior Information Developer
Business Analytics (SPSS)
E-mail: [hidden email]





From:        
Martha Hewett <[hidden email]>
To:        
[hidden email]
Date:        
11/22/2011 03:34 PM
Subject:        
How can I convert a date to a string version of itself?
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>




I have a variable for site ID, and another variable for date.  Each site and date then has a bunch of records associated with it.  I want to create a variable that will be an identifier for each site visit, e.g. a particular value of that variable might be 1027_Feb_11_2009 for site 1027, the visit of Feb 11, 2009.  This is probably obvious but my attempts have not been working.



Martha Hewett
Director of Research | 612.335.5865
Center for Energy and Environment
212 Third Avenue North, Suite 560 | Minneapolis, MN 55401