Solutions - Re: Converting time stored as a string variable to a date variable (TIME5)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Solutions - Re: Converting time stored as a string variable to a date variable (TIME5)

ariel barak
Everyone,

I received two solutions to my question. First, the one sent to me by
Michael Roberts assumes that there are always leading zero's which is what
I asked for . Thanks for your help.

data list / assaulttime (A4).
begin data
0145
0728

1334
end data.
compute as_time=time.hms(numeric(substr(assaulttime,1,2),f2.0
),numeric(substr(assaulttime,3,2),f2.0)).
formats as_time (time5).
exe.


Second solution assumes that there may not always be leading zero's. This
solution was submitted by Mark Antro. Thanks for your help.

data list / assaulttime (A4).
begin data
0145
0728

1334
145
end data.

STRING newTime (A5).
DO IF LENGTH(RTRIM(assaulttime)) = 4.
+   COMPUTE newTime = CONCAT(SUBSTR(assaulttime ,1,2), ":",
SUBSTR(assaulttime,3)).
ELSE IF LENGTH(RTRIM(assaulttime)) = 3.
+   COMPUTE newTime = CONCAT(SUBSTR(assaulttime ,1,1), ":",
SUBSTR(assaulttime,2)).
END IF.
COMPUTE assaulttime2 = number(newTime , TIME5).
VARIABLE LABEL assaulttime2 .
VARIABLE LEVEL assaulttime2 (SCALE).
FORMATS assaulttime2 (TIME5).
VARIABLE WIDTH assaulttime2 (5).
EXE.


Thanks again Michael and Mark for your help!


On 9/19/06, ariel barak <[hidden email] > wrote:

>
>  Hello SPSS members,
>
> I have time data (military) with some missing values in the format below
> stored as a string variable. I have written the syntax below which converts
> the data from 0145 into 01:45 properly. I can easily convert the String
> variable to the proper TIME5 date format via the GUI but have run into
> nothing but trouble trying to do this via syntax. I'm sure there is a very
> simple solution.
>
> Thanks for your help.
>
> -Ari
>
>
> data list / assaulttime (A4).
> begin data
> 0145
> 0728
>
> 1334
> end data.
>
> STRING hour (A2).
> STRING minute (A2).
> COMPUTE hour = SUBSTR(assaulttime, 1, 2).
> COMPUTE minute = SUBSTR(assaulttime, 3, 2).
> EXE.
>
> STRING assaulttime2 (A5).
> COMPUTE assaulttime2 = CONCAT(hour, ':', minute).
> IF assaulttime2 = '  :' assaulttime2 = ''.
> EXE.
>
Reply | Threaded
Open this post in threaded view
|

syntax editor

Sebastián Daza
dear list,
with tdRunSyntax comes a very good documentation that shows how to use it together with TextPad. However, my favorite editor is UltraEdit. Can I use tdRunSyntax with this editor, as well?  Can you explain/show me how to configure UltraEdit so that I can submit SPSS commands from this editor in a similar way as from TextPad?

--
Sebastián Daza Aranzaes

Sebastián Daza Aranzaes
Instituto de Sociología UC
8-471 53 87 / 686 57 20 / Fax 5521834
[hidden email]