converting date

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

converting date

Stella Vasquez

I’m trying to convert a date variable (Adate10) to a 8 characters variable with no slashes.  So a date of birth of 2/23/1998 would be 19980223.

 

My problem is when I separate the month and day, I get a number variable and I need a two digit month and day.

Any help would be appreciated.

 

Thanks.

 

 

Stella Vasquez, Planner

Research & Development

1624 W Adams,

Phoenix, AZ 85007

602-542-2272

 



**The information contained in this communication is privileged and confidential and is intended solely for the individual[s] and/or entities named herein. This information is not to be disseminated. If you have received this message in error, please reply to the sender and notify the sender of the error and then permanently delete the message and sent item. Thank you.**
Reply | Threaded
Open this post in threaded view
|

Re: converting date

Rick Oliver-3
You could make this more parsimonious, but here's the general idea:

data list list /datevar (adate10).
begin data
2/23/1998
end data.
compute #year=xdate.year(datevar).
compute #month=xdate.month(datevar).
compute #day=xdate.mday(datevar).
string #tempstring (a10).
compute #tempstring=concat(string(#year, f4), string(#month, n2), string(#day, n2)).
compute numdate=number(#tempstring,f10).
formats numdate (f10).
list.


Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]
Phone: 312.893.4922 | T/L: 206-4922




From:        Stella Vasquez <[hidden email]>
To:        [hidden email]
Date:        03/22/2012 03:29 PM
Subject:        converting date
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I’m trying to convert a date variable (Adate10) to a 8 characters variable with no slashes.  So a date of birth of 2/23/1998 would be 19980223.
 
My problem is when I separate the month and day, I get a number variable and I need a two digit month and day.
Any help would be appreciated.
 
Thanks.
 
 
Stella Vasquez, Planner
Research & Development
1624 W Adams,
Phoenix, AZ 85007
602-542-2272
 



**The information contained in this communication is privileged and confidential and is intended solely for the individual[s] and/or entities named herein. This information is not to be disseminated. If you have received this message in error, please reply to the sender and notify the sender of the error and then permanently delete the message and sent item. Thank you.**

Reply | Threaded
Open this post in threaded view
|

Re: converting date

Bruce Weaver
Administrator
In reply to this post by Stella Vasquez
XDATE and ** are your friends.

data list list / mydate (adate10).
begin data
2/23/1998
2/3/1998
12/3/1998
12/23/1998
end data.

compute NewDate = xdate.year(mydate)*10**4 + xdate.month(mydate)*10**2 + xdate.mday(mydate).
formats NewDate(f8.0).
LIST.

Output:
 mydate  NewDate
 
02/23/1998 19980223
02/03/1998 19980203
12/03/1998 19981203
12/23/1998 19981223
 
Number of cases read:  4    Number of cases listed:  4


Stella Vasquez wrote
I'm trying to convert a date variable (Adate10) to a 8 characters variable with no slashes.  So a date of birth of 2/23/1998 would be 19980223.

My problem is when I separate the month and day, I get a number variable and I need a two digit month and day.
Any help would be appreciated.

Thanks.


Stella Vasquez, Planner
Research & Development
1624 W Adams,
Phoenix, AZ 85007
602-542-2272


________________________________
**The information contained in this communication is privileged and confidential and is intended solely for the individual[s] and/or entities named herein. This information is not to be disseminated. If you have received this message in error, please reply to the sender and notify the sender of the error and then permanently delete the message and sent item. Thank you.**
--
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: converting date

David Marso
Administrator
In reply to this post by Stella Vasquez
Stella,
  It is *ALWAYS* easier to mend whatever slightly dodgy already existing syntax than it is to
rebuild whatever parsing you have already scraped together.
Hence, I will merely leave you with guidance to explore the N format for numeric fields:
COMPUTE AlphaX=STRING(NumVar,N2).
 -> 02: NumVar=2!
HTH, David

Stella Vasquez wrote
I'm trying to convert a date variable (Adate10) to a 8 characters variable with no slashes.  So a date of birth of 2/23/1998 would be 19980223.

My problem is when I separate the month and day, I get a number variable and I need a two digit month and day.
Any help would be appreciated.

Thanks.


Stella Vasquez, Planner
Research & Development
1624 W Adams,
Phoenix, AZ 85007
602-542-2272


________________________________
**The information contained in this communication is privileged and confidential and is intended solely for the individual[s] and/or entities named herein. This information is not to be disseminated. If you have received this message in error, please reply to the sender and notify the sender of the error and then permanently delete the message and sent item. Thank you.**
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: converting date

Bruce Weaver
Administrator
But notice that all Stella really wants to do is "convert a date variable (Adate10) to a 8 characters variable with no slashes" such that 2/23/1998 is converted to 19980223.  In that case, there's no need to get into N formats.  The one-line COMPUTE (with XDATE functions and ** operators) I posted earlier does the trick.  ;-)


David Marso wrote
Stella,
  It is *ALWAYS* easier to mend whatever slightly dodgy already existing syntax than it is to
rebuild whatever parsing you have already scraped together.
Hence, I will merely leave you with guidance to explore the N format for numeric fields:
COMPUTE AlphaX=STRING(NumVar,N2).
 -> 02: NumVar=2!
HTH, David

Stella Vasquez wrote
I'm trying to convert a date variable (Adate10) to a 8 characters variable with no slashes.  So a date of birth of 2/23/1998 would be 19980223.

My problem is when I separate the month and day, I get a number variable and I need a two digit month and day.
Any help would be appreciated.

Thanks.


Stella Vasquez, Planner
Research & Development
1624 W Adams,
Phoenix, AZ 85007
602-542-2272


________________________________
**The information contained in this communication is privileged and confidential and is intended solely for the individual[s] and/or entities named herein. This information is not to be disseminated. If you have received this message in error, please reply to the sender and notify the sender of the error and then permanently delete the message and sent item. Thank you.**
--
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: converting date

David Marso
Administrator
Ah, that's true:  Your post had not appeared at the time I posted my hint.
I had ASSumed that she had parsed out 3 fields and concat'ed to get the result, leading to single char mo or day elements ;-)
OTOH here is a 'straightforward;-)' string parsing approach.

data list list / mydate (adate10).
begin data
2/23/1998
2/3/1998
12/3/1998
12/23/1998
end data.
STRING strDate(A10).
COMPUTE strDate=STRING(mydate,ADATE10) .
COMPUTE #1=CHAR.INDEX(strDate,"/") .
COMPUTE #2=CHAR.RINDEX(strDate,"/") .
COMPUTE strDATE=CONCAT(CHAR.SUBSTR(strDATE,#2+1),
                                         CHAR.SUBSTR(strDate,1,#1-1),
                                         CHAR.SUBSTR(strdate,#1+1,#2-#1-1)).
ALTER TYPE strdate(A8).
Bruce Weaver wrote
But notice that all Stella really wants to do is "convert a date variable (Adate10) to a 8 characters variable with no slashes" such that 2/23/1998 is converted to 19980223.  In that case, there's no need to get into N formats.  The one-line COMPUTE (with XDATE functions and ** operators) I posted earlier does the trick.  ;-)


David Marso wrote
Stella,
  It is *ALWAYS* easier to mend whatever slightly dodgy already existing syntax than it is to
rebuild whatever parsing you have already scraped together.
Hence, I will merely leave you with guidance to explore the N format for numeric fields:
COMPUTE AlphaX=STRING(NumVar,N2).
 -> 02: NumVar=2!
HTH, David

Stella Vasquez wrote
I'm trying to convert a date variable (Adate10) to a 8 characters variable with no slashes.  So a date of birth of 2/23/1998 would be 19980223.

My problem is when I separate the month and day, I get a number variable and I need a two digit month and day.
Any help would be appreciated.

Thanks.


Stella Vasquez, Planner
Research & Development
1624 W Adams,
Phoenix, AZ 85007
602-542-2272


________________________________
**The information contained in this communication is privileged and confidential and is intended solely for the individual[s] and/or entities named herein. This information is not to be disseminated. If you have received this message in error, please reply to the sender and notify the sender of the error and then permanently delete the message and sent item. Thank you.**
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?"