Date transformation

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

Date transformation

Mark Palmberg-2
I have a numeric field from a database import that's 10 characters and
in the format 11051991 (for November 5, 1991).  But there are also
values like 3151985 (no leading 0) for March 15, 1985. When I try to
apply a date field format as mm/dd/yyyy, all the dates end up sometime
in the 1500s.  How do I make these dates useable?  This is in SPSS 15.
 
Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Date transformation

Oliver, Richard
They all end up as dates in the 1500s because the numeric values are converted to dates based on the assumption that the numbers represent the number of seconds since October 14, 1582. There are method for parsing values and converting them to dates -- but it is problematic if there are ambiguous or inconsistently entered values. The lack of a leading zero at the start of the number isn't particularly problematic, but if day values less than 10 also don't have leading zeroes, then that is problematic. For example, what does 1012006 represent? October 1 or January 1?

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mark Palmberg
Sent: Friday, August 24, 2007 10:47 AM
To: [hidden email]
Subject: Date transformation

I have a numeric field from a database import that's 10 characters and
in the format 11051991 (for November 5, 1991).  But there are also
values like 3151985 (no leading 0) for March 15, 1985. When I try to
apply a date field format as mm/dd/yyyy, all the dates end up sometime
in the 1500s.  How do I make these dates useable?  This is in SPSS 15.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Date transformation

Oliver, Richard
In reply to this post by Mark Palmberg-2
Assuming day values less than 10 have leading zeroes, something like this should work:

data list list /numdate (f10).
begin data
01012006
02012006
3012006
end data.
string #temp (a8).
compute #temp=string(numdate,N8).
compute #month=number(substr(#temp,1,2), f2).
compute #day=number(substr(#temp, 3, 2), f2).
compute #year=number(substr(#temp, 5, 4), f4).
compute date=date.mdy(#month, #day, #year).
formats date (adate10).
list.

It can be done in fewer lines of code, I just find this easier to interpret and debug.

#temp is a string containing the numeric date value. N8 formats insures that 7-digit values (ones with no leading zero for month) will have a leading zero in the string value.

Then we take the various parts of #temp and create numeric variables that contain the first two digits (#month), the second two digits (#day), and the last four digits (#year).

Finally, we use the date.mdy function to create a real date from the three parts -- but make sure to assign a date format or the value will display as a very large integer (number of second since October 14, 1582).

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mark Palmberg
Sent: Friday, August 24, 2007 10:47 AM
To: [hidden email]
Subject: Date transformation

I have a numeric field from a database import that's 10 characters and
in the format 11051991 (for November 5, 1991).  But there are also
values like 3151985 (no leading 0) for March 15, 1985. When I try to
apply a date field format as mm/dd/yyyy, all the dates end up sometime
in the 1500s.  How do I make these dates useable?  This is in SPSS 15.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Date transformation

Fry, Jonathan B.
In reply to this post by Mark Palmberg-2
-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mark Palmberg
Sent: Friday, August 24, 2007 10:47 AM
To: [hidden email]
Subject: Date transformation

I have a numeric field from a database import that's 10 characters and
in the format 11051991 (for November 5, 1991).  But there are also
values like 3151985 (no leading 0) for March 15, 1985. When I try to
apply a date field format as mm/dd/yyyy, all the dates end up sometime
in the 1500s.  How do I make these dates useable?  This is in SPSS 15.

Thanks.
----------------------

Richard Oliver (a writer by trade) has suggested a nice text-based solution.  I'm more math-oriented, so I'll suggest an arithmetic solution.

compute #mo = trunc(date/1e6).
compute #da = trunc(mod(date,1e6)/1e4).
compute #yr = mod(date,1e4).
compute newdate = date.mdy(#mo,#da,#yr).
format newdate(adate10).

Jonathan Fry
SPSS Inc.
Reply | Threaded
Open this post in threaded view
|

Re: Date transformation

Oliver, Richard
D'oh! Jon's solution is, of course, much cleaner.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Fry, Jonathan B.
Sent: Monday, August 27, 2007 11:35 AM
To: [hidden email]
Subject: Re: Date transformation

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mark Palmberg
Sent: Friday, August 24, 2007 10:47 AM
To: [hidden email]
Subject: Date transformation

I have a numeric field from a database import that's 10 characters and
in the format 11051991 (for November 5, 1991).  But there are also
values like 3151985 (no leading 0) for March 15, 1985. When I try to
apply a date field format as mm/dd/yyyy, all the dates end up sometime
in the 1500s.  How do I make these dates useable?  This is in SPSS 15.

Thanks.
----------------------

Richard Oliver (a writer by trade) has suggested a nice text-based solution.  I'm more math-oriented, so I'll suggest an arithmetic solution.

compute #mo = trunc(date/1e6).
compute #da = trunc(mod(date,1e6)/1e4).
compute #yr = mod(date,1e4).
compute newdate = date.mdy(#mo,#da,#yr).
format newdate(adate10).

Jonathan Fry
SPSS Inc.