This is not a question, but rather an attempt to point at some nifty features of the built-in functions for dealing with dates and times. At least in my work, this is a very frequent source of difficulties.
To be more specific, the way SPSS can convert dates formatted as text into working dates is quite fascinating. Almost any reasonable format can be handled. And it works even when the text is formatted differently within the same variable.
I have put together a short example (where EXECUTE should be inserted after COMPUTE and IF commands if temporary stops are wanted):
***********************************************************.
DATA LIST LIST(";")/date(A24) time(A20).
BEGIN DATA
November 13 2024 ;03:45 AM
Oct 4 2024; 03:45PM
Nov 14 2024 ; 11:15AM
12 24 2024; 11:15 PM
11/25/2024;4:56 PM
END DATA.
*This would of course be a silly way to set up the data, but the point is that many formats can be dealt with for date variables,
*as long as the days, months and year details come in the order needed for a specific format.
*Further, instead of the following COMPUTE command, the date format could be specified from the outset in the
*DATA LIST command: DATA LIST LIST/date(ADATE20) time(A20). Which also can be specified when importing text files through the menus.
*Convert the text into working dates.
COMPUTE date2=NUMBER(date,ADATE20).
FORMATS date2(ADATE10).
*Now to the times, a bit more cumbersome than the dates.
*Some cleaning of the times, remove leading or trailing blanks.
COMPUTE time=LTRIM(RTRIM(time)).
*More cleaning, remove blanks.
COMPUTE time=REPLACE(time,' ','').
*Make the time variable as small as possible.
ALTER TYPE time(A=AMIN).
*Insert a leading 0 when needed.
COMPUTE time=CHAR.LPAD(time,7,'0').
*Convert the times as strings to working time variables.
IF CHAR.SUBSTR(time,6,2)='AM' time2=NUMBER(CHAR.SUBSTR(time,1,5),TIME5).
IF CHAR.SUBSTR(time,6,2)='PM' time2=NUMBER(CHAR.SUBSTR(time,1,5),TIME5)+12*60*60.
FORMATS time2(TIME5).
COMPUTE datetime=date2+time2.
EXECUTE.
FORMATS datetime(DATETIME17).
*************************************************.
The date conversion, isn't it fascinating? How can such messy data be converted so easily?
------------------------------
Robert Lundqvist
------------------------------