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
30
31
32
33
34
35
36
37
38
39
Title:
Changing string variable with AM and PM to military time in SPSS 
Description:
Q. 
I am using SPSS and have received an SPSS data file in system file (.SAV) 
or portable file (.POR) format from another individual. The problem is the 
time variable is in string format with AM and PM designations and I need to 
change it to military format. How can I do this? 
A. 
Following is an example showing how this procedure can be accomplished 
through SPSS Command Syntax. 
(This is a modification by Ray of a syntax originally posted as Solution ID 100006716)

*THIS PART OF THE SYNTAX WILL CREATE A SAMPLE DATA SET. DO NOT USE 
*THIS PART OF THE SYNTAX WITH YOUR DATA FILE. 
DATA LIST /oldtime 1-8 (a). 
BEGIN DATA 
3:10 PM
 3:10 PM
12:20 AM 
4:55 AM 
10:12 PM 
12:08 PM 
END DATA. 

*THIS SYNTAX IS WHAT YOU NEED FOR YOUR DATA FILE. MAKE SURE TO 
*CHANGE THE NAME 'OLDTIME' TO THE NAME OF YOUR STRING VARIABLE 
*IN YOUR SYNTAX. THIS SYNTAX WILL CREATE A NEW VARIABLE NAMED 
*NEWTIME. THIS IS THE VARIABLE THAT WILL CONTAIN THE FORMATTED 
*MILITARY TIME. 

COMPUTE newtime=NUM(SUBSTR(oldtime,1,5),time). 
DO IF (INDEX(oldtime,'PM')>0  AND NOT (SUBSTR(oldtime,1,2)='12')). 
- COMPUTE NEWTIME=NEWTIME + 43200. 
ELSE IF ((SUBSTR(oldtime,1,2)='12') AND INDEX(oldtime,'AM')>0). 
- COMPUTE NEWTIME=NEWTIME - 43200. 
END IF. 
FORMATS newtime (time). 
EXE.