Troubleshooting
Problem
How to convert Standard Time to Military Time. The Statndard Time is represented as a String Variable.
Resolving The Problem
Following is an example showing how to take a Standard Time variable String and convert it to Military Time, through SPSS Command Syntax.
*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 /StartTime 1-8 (a).
BEGIN DATA
3:10 PM
12:20 AM
4:55 AM
10:12 PM
12:08 PM
4:10 PM
END DATA.
* This example takes a 8 character Standard Time string field (StartTime) and converts the string to Military Time.
* This example takes into account whether the Standard Time String Variable data is preceded with or without a blank.
COMPUTE StartTime = LPAD(RTRIM(LTRIM(StartTime)),8,' ').
EXECUTE.
COMPUTE MilStartTime=NUM(SUBSTR(StartTime,1,5),time).
DO IF ((SUBSTR(StartTime,7,2)='PM')
AND NOT (SUBSTR(StartTime,1,2)='12')).
COMPUTE MilStartTime= MilStartTime+43200.
Else IF
((SUBSTR(StartTime,1,2)='12') AND (SUBSTR(StartTime,7,2)='AM')).
COMPUTE MilStartTime=MilStartTime-43200.
END IF.
FORMATS MilStartTime (time).
EXECUTE.
Related Information
Historical Number
17536
Was this topic helpful?
Document Information
Modified date:
16 April 2020
UID
swg21476174