Date format for yyyymmdd

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

Date format for yyyymmdd

Staffan Lindberg

Dear list!

 

I have a lot of time variables, defined as numerical variables (i.e. 19920305,20040617). This is the most common date format in Scandinavian countries (as an alternative yymmdd). How do I convert this into a SPSS date format, thus permitting me to use different date functions (datediff etc.)?

 

best

 

Staffan Lindberg

Sweden

Reply | Threaded
Open this post in threaded view
|

Re: Date format for yyyymmdd

Art Kendall
Try this.

data list free /mystring(a8).
begin data
19990131
990131
19461212
123
end data.
LIST .
compute howlong = length(ltrim(rtrim(mystring))).
do if howlong eq 8.
compute myday = number(subst(ltrim(rtrim(mystring)),7,2),f2).
compute mymon = number(subst(ltrim(rtrim(mystring)),5,2),f2).
compute myyr  = number(subst(ltrim(rtrim(mystring)),1,4),f4).
else if howlong eq 6.
compute myday = number(subst(ltrim(rtrim(mystring)),5,2),f2).
compute mymon = number(subst(ltrim(rtrim(mystring)),3,2),f2).
compute myyr  = number(subst(ltrim(rtrim(mystring)),1,2),f2).
ELSE  .
print/ 'oops'.
end if.
compute mydate = data.mdy(mymon, myday,myyr).
formats mydate(date10).
list vars= mystring mydate.

Art Kendall
Social Research Consultants

Staffan Lindberg wrote:

Dear list!

 

I have a lot of time variables, defined as numerical variables (i.e. 19920305,20040617). This is the most common date format in Scandinavian countries (as an alternative yymmdd). How do I convert this into a SPSS date format, thus permitting me to use different date functions (datediff etc.)?

 

best

 

Staffan Lindberg

Sweden

===================== 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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Date format for yyyymmdd

Eero Olli
In reply to this post by Staffan Lindberg
Changing the date format from yyyymmdd can be a bit tedious.
Perhaps you can use one of these codings below, which work fine in SPSS
v15.


Best,
Eero Olli

* Change a numeric YYYYMMDD datevariable to E10Date (DD.YY.YYYY) .
* by Eero Olli.
* all string variables here are temporary variables.
* thus it is possible to use the same code for several datevariables.
* as long as there is an execute command between them.
* "olddate" is the variable to be revamped.
* the empty line in the data list is on purpose, because I like to
check.
* how the syntax deals with missing values.


DATA LIST FREE /OldDate (F8.0)  .
BEGIN DATA
20001230
20090125

20090713
END DATA.

DATASET NAME datetransform WINDOW = front.


ECHO "LONG FORM for understanding what happens.".
STRING #Olddate_string (A8) #year_string (A4) #month_string #date_string
(A2).
DO IF olddate>0.
+ * make a string date.
+ COMPUTE #Olddate_string = STRING(Olddate, F8.0).
+ COMPUTE #Year_string= SUBSTR(#Olddate_string, 1,4) .
+ COMPUTE #Month_string = SUBSTR(#Olddate_string, 5,2) .
+ COMPUTE #Date_string =  SUBSTR(#Olddate_string, 7,2) .
+ * put the pieces together again.
+ COMPUTE NewDate=
        DATE.dmy(Number(#date_string ,F2.0)  ,
                 Number(#month_string,F2.0)  ,
                 Number(#year_string ,F4.0)  ) .
END IF.
VARIABLE LABEL NewDate "The NewDate in a European format".
VARIABLE LEVEL NewDate(SCALE).
FORMATS NewDate(EDATE10).
VARIABLE WIDTH newdate (10).
EXECUTE.

LIST.


ECHO "SHORT FORM replaces the variable directly".
ECHO "This is bad habit if not properly checked for errors.".
COMPUTE olddate =
        DATE.dmy(Number(SUBSTR(STRING(Olddate, F8.0), 7,2) ,F2.0)  ,
                 Number(SUBSTR(STRING(Olddate, F8.0), 5,2) ,F2.0)  ,
                 Number(SUBSTR(STRING(Olddate, F8.0), 1,4) ,F4.0)  ) .
VARIABLE LABEL olddate "The olddate in a European format".
VARIABLE LEVEL olddate (SCALE).
FORMATS olddate (EDATE10).
VARIABLE WIDTH olddate (10).
EXECUTE.

LIST.



________________________________________
Eero Olli
Advisor
the Equality and Anti-discrimination Ombud
[hidden email]                   +47 2405 5951
POB 8048 Dep,     N-0031 Oslo,      Norway

=====================
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