convert string to numeric date

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

convert string to numeric date

leah rubin
Dear ListServ,

I have a string date variable in the form mmyy. I would like to transform this variable into mmddyyyy
where I specify dd = 15.

Thanks in advance,
Leah
Reply | Threaded
Open this post in threaded view
|

Re: convert string to numeric date

Maguin, Eugene

Leah,

 

Let the current string date variables be x and the new one be y. A key problem is whether ‘yy’ refers to 19yy or 20yy. In the interest of generality, let’s say both.

String y(a8).

Do if (substr(x,3,2) le ‘13’).

Compute y=concat(substr(x,1,2),’1520’, substr(x,3,2)).

Else.

Compute y=concat(substr(x,1,2),’1519’, substr(x,3,2)).

End if.

 

 

Gene Maguin

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of leah rubin
Sent: Tuesday, February 07, 2012 2:29 PM
To: [hidden email]
Subject: convert string to numeric date

 

Dear ListServ,

I have a string date variable in the form mmyy. I would like to transform this variable into mmddyyyy
where I specify dd = 15.

Thanks in advance,
Leah

Reply | Threaded
Open this post in threaded view
|

Re: convert string to numeric date

David Marso
Administrator
There is *NO* numeric date format of the flavor specified (MMDDYYY) without separators!
My solution which returns a true date and works for both 19xx and 20xx dates.
data list free / strdate (A4).
begin data
0112  1110  0995
end data.
STRING #EPOCH (A2).
COMPUTE #EPOCH="20".
IF SUBSTR(strdate,3)<"00"|SUBSTR(strdate,3)>"12"  #EPOCH="19".
compute numdate=number(CONCAT(SUBSTR(strdate,1,2),"-15-",#EPOCH,SUBSTR(strdate,3)),ADATE).      
FORMATS numdate (ADATE).
list.

STRDATE    NUMDATE

0112    01/15/2012
1110    11/15/2010
0995    09/15/1995


Number of cases read:  3    Number of cases listed:  3
Gene Maguin wrote
Leah,



Let the current string date variables be x and the new one be y. A key
problem is whether 'yy' refers to 19yy or 20yy. In the interest of
generality, let's say both.

String y(a8).

Do if (substr(x,3,2) le '13').

Compute y=concat(substr(x,1,2),'1520', substr(x,3,2)).

Else.

Compute y=concat(substr(x,1,2),'1519', substr(x,3,2)).

End if.





Gene Maguin



From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
leah rubin
Sent: Tuesday, February 07, 2012 2:29 PM
To: [hidden email]
Subject: convert string to numeric date



Dear ListServ,

I have a string date variable in the form mmyy. I would like to transform
this variable into mmddyyyy
where I specify dd = 15.

Thanks in advance,
Leah
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?"