1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
* (Q) Extract the time portion from a string including date and time.
* (A) Ray 2001/10/31.

DATA LIST FIXED /start 1-19(A).
BEGIN DATA
08/01/2001 10:15 AM
08/10/2001 12:00 PM
08/15/2001 09:15 AM
08/20/2001 11:45 PM
END DATA.
LIST.

* String variables must be defined before being used.
STRING timest(A5) ampm(A2).
COMPUTE timest=SUBSTR(start,INDEX(start,":") -2).
COMPUTE time1=NUMBER(timest,"TIME5").
FORMATS time1(TIME5).

COMPUTE ampm=SUBSTR(start,18).
* Time is kept in seconds in SPSS.
* IF PM, add 12 hours * 60 minutes * 60 seconds to the AM time.
DO IF ampm="PM".
COMPUTE time2=time1+12*60*60.
ELSE.
COMPUTE time2=time1.
END IF.

FORMATS time2(TIME5).
EXECUTE.