LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (February 2006)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sat, 4 Feb 2006 14:07:31 -0600
Reply-To:     "Oliver, Richard" <roliver@SPSS.COM>
Sender:       "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:         "Oliver, Richard" <roliver@SPSS.COM>
Subject:      Re: format question-- time variable
Comments: To: Richard Ristow <wrristow@mindspring.com>
Content-Type: text/plain; charset="iso-8859-1"

Given the case in which the original value only contains a simple 24-hour clock time in TIME format, the code below should be sufficient (although I haven't tested in rigorously). This won't work for value that are, in fact, datetimes, but have a time display format that displays only the time portion of the datetime (same for dtime). There are probably more elegant solutions. *generate some test data. data list free /time1 (time5). begin data 12:01 00:05 10:04 15:45 12:00 0:00 12:59 0:59 1:00 end data. *now start the real job. numeric #newtime(time5). string #ampm (a2). compute #temp=60*60*12. do if xdate.hour(time1)=12. - compute #newtime=time1. - compute #ampm="PM". else if xdate.hour(time1)=0. - compute #newtime=time1+#temp. - compute #ampm="AM". else if xdate.hour(time1)>12. - compute #newtime=time1-#temp. - compute #ampm="PM". else if xdate.hour(time1)<12. - compute #newtime=time1. - compute #ampm="AM". end if. string stringtime (a8). compute stringtime=concat(string(#newtime, time5), " ", #ampm).

________________________________

From: SPSSX(r) Discussion on behalf of Richard Ristow Sent: Fri 2/3/2006 7:46 PM To: SPSSX-L@LISTSERV.UGA.EDU Subject: Re: format question-- time variable

At 03:22 PM 2/3/2006, Marks, Jim wrote:

>what format do I use to show time variables as 11:01 am or 9:33 pm? >(the variable has format "time5" currently)

Should be obvious, maybe even possible >:-} - but SPSS has no time or date-time format that will display times with am/pm; See list of formats, SPSS 14 Command Syntax Reference, pp. 66-67. I'm not sure why that omission has never been recognized and rectified. But since I've got it, here's syntax to convert an SPSS time variable to a string with AM/PM.(*) This is the whole program for the posting, including test code.

The output string is variable CVT. The code assumes a date-time variable; for a pure time variable, you don't need the part that handles the date. ............................................ For converting 24-hour time to 12-hour with AM/PM, there are FOUR cases: . Midnight to 1:00 a.m. . 1:00 a.m. to 12:00 noon . 12:00 noon to 1:00 p.m. . 1:00 p.m. to midnight.

This is tested; draft output listing.

In the test data, most of the lines are date/time values I constructed for the purpose, but the first one uses $TIME, the system value with the date-time when it is run.

/* ------------ Test data ------------ */ NEW FILE. INPUT PROGRAM. . NUMERIC TIME_VAL (DATETIME22). . VARIABLE WIDTH TIME_VAL (22). . VARIABLE ALIGNMENT TIME_VAL (CENTER).

. COMPUTE TIME_VAL = $TIME. . END CASE. . COMPUTE TIME_VAL = DATE.MDY(06,06,1944) + TIME.HMS(10,34,56). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(02,14,1966) + TIME.HMS(15,54,32). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(01,10,2001) + TIME.HMS(13,23,45). . END CASE.

. COMPUTE TIME_VAL = DATE.MDY(07,03,2002) + TIME.HMS(23,30,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(00,00,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(00,30,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(01,00,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(01,30,00). . END CASE.

. COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(11,30,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(12,00,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(12,30,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(13,00,00). . END CASE. . COMPUTE TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(13,30,00). . END CASE. END FILE. END INPUT PROGRAM.

/* ------------ Desired format ------------ */ /* Target output variable */ STRING CVT (A22). /* Scratch intermediate variables */ STRING #DATE (A10). NUMERIC #TIME (TIME10) /#ATIME (TIME10). STRING #HMS (A08) /#AMPM (A02).

COMPUTE #DATE = STRING(TIME_VAL,ADATE10).

COMPUTE #TIME = XDATE.TIME(TIME_VAL). DO IF (#TIME < TIME.HMS(01)). . COMPUTE #ATIME = #TIME + TIME.HMS(12). . COMPUTE #HMS = STRING(#ATIME,TIME08). . COMPUTE #AMPM = 'AM'. ELSE IF (#TIME < TIME.HMS(12)). . COMPUTE #ATIME = #TIME. . COMPUTE #HMS = STRING(#ATIME,TIME08). . COMPUTE #AMPM = 'AM'. ELSE IF (#TIME < TIME.HMS(13)). . COMPUTE #ATIME = #TIME. . COMPUTE #HMS = STRING(#ATIME,TIME08). . COMPUTE #AMPM = 'PM'. ELSE. . COMPUTE #ATIME = #TIME - TIME.HMS(12). . COMPUTE #HMS = STRING(#ATIME,TIME08). . COMPUTE #AMPM = 'PM'. END IF.

COMPUTE CVT = CONCAT(#DATE,' ',#HMS,' ',#AMPM).

/* ------------ Display ------------ */ * Scratch variables as permanent variables, for display . NUMERIC NUM_TM (TIME10) /NUM_ADJT(TIME10). STRING STR_DT (A10) /STR_HMS (A08) /AM (A02). COMPUTE STR_DT = #DATE. COMPUTE NUM_TM = #TIME. COMPUTE NUM_ADJT = #ATIME. COMPUTE STR_HMS = #HMS. COMPUTE AM = #AMPM.

STRING X (A2). COMPUTE X = ' '. EXECUTE. LIST VARIABLES=TIME_VAL X CVT.

List TIME_VAL X CVT

31-OCT-2003 00:01:26 10/31/2003 12:01:26 AM 06-JUN-1944 10:34:56 06/06/1944 10:34:56 AM 14-FEB-1966 15:54:32 02/14/1966 3:54:32 PM 10-JAN-2001 13:23:45 01/10/2001 1:23:45 PM 03-JUL-2002 23:30:00 07/03/2002 11:30:00 PM 04-JUL-2002 00:00:00 07/04/2002 12:00:00 AM 04-JUL-2002 00:30:00 07/04/2002 12:30:00 AM 04-JUL-2002 01:00:00 07/04/2002 1:00:00 AM 04-JUL-2002 01:30:00 07/04/2002 1:30:00 AM 04-JUL-2002 11:30:00 07/04/2002 11:30:00 AM 04-JUL-2002 12:00:00 07/04/2002 12:00:00 PM 04-JUL-2002 12:30:00 07/04/2002 12:30:00 PM 04-JUL-2002 13:00:00 07/04/2002 1:00:00 PM 04-JUL-2002 13:30:00 07/04/2002 1:30:00 PM

Number of cases read: 14 Number of cases listed: 14

LIST VARIABLES=TIME_VAL X STR_DT NUM_TM NUM_ADJT STR_HMS AM.

List TIME_VAL X STR_DT NUM_TM NUM_ADJT STR_HMS AM

31-OCT-2003 00:01:26 10/31/2003 0:01:26 12:01:26 12:01:26 AM 06-JUN-1944 10:34:56 06/06/1944 10:34:56 10:34:56 10:34:56 AM 14-FEB-1966 15:54:32 02/14/1966 15:54:32 3:54:32 3:54:32 PM 10-JAN-2001 13:23:45 01/10/2001 13:23:45 1:23:45 1:23:45 PM 03-JUL-2002 23:30:00 07/03/2002 23:30:00 11:30:00 11:30:00 PM 04-JUL-2002 00:00:00 07/04/2002 0:00:00 12:00:00 12:00:00 AM 04-JUL-2002 00:30:00 07/04/2002 0:30:00 12:30:00 12:30:00 AM 04-JUL-2002 01:00:00 07/04/2002 1:00:00 1:00:00 1:00:00 AM 04-JUL-2002 01:30:00 07/04/2002 1:30:00 1:30:00 1:30:00 AM 04-JUL-2002 11:30:00 07/04/2002 11:30:00 11:30:00 11:30:00 AM 04-JUL-2002 12:00:00 07/04/2002 12:00:00 12:00:00 12:00:00 PM 04-JUL-2002 12:30:00 07/04/2002 12:30:00 12:30:00 12:30:00 PM 04-JUL-2002 13:00:00 07/04/2002 13:00:00 1:00:00 1:00:00 PM 04-JUL-2002 13:30:00 07/04/2002 13:30:00 1:30:00 1:30:00 PM

Number of cases read: 14 Number of cases listed: 14 ............................................ (*) Posting "Re: $time AM/PM", Richard Ristow, Fri, 31 Oct 2003 00:05:42 -0500

-- Internal Virus Database is out-of-date. Checked by AVG Anti-Virus. Version: 7.1.375 / Virus Database: 267.14.23/243 - Release Date: 1/27/2006


Back to: Top of message | Previous page | Main SPSSX-L page