Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: problem using -clock- with military time

From   Nick Cox <>
Subject   Re: st: problem using -clock- with military time
Date   Sat, 2 Jun 2012 00:00:31 +0100

input str15 ArrivedOnPCU
"2011/04/06 1630"
"2010/07/18 700"
"2011/09/06 400"
"2011/06/23 130"
replace Arrived = trim(Arrived)
replace Arrived = subinstr(Arrived, " ", " 0", 1) if
length(word(Arrived, -1)) == 3

This example boosts my prejudice that few parts of Stata are so
unfairly overlooked as the basic string functions. See also

Cox, N.J. 2011. Speaking Stata: Fun and fluency with functions. The
Stata Journal 11(3): 460-471

Abstract.  Functions are the unsung heroes of Stata. This column is a
tour of functions that might easily be missed or underestimated, with
a potpourri of tips, tricks, and examples for a wide range of basic

for a review.

On Fri, Jun 1, 2012 at 11:39 PM, Steve Nakoneshny <> wrote:

> I have been provided with a dataset containing date and time variables in string format. I wish to convert these to SIF type using the -clock- function, however I have run into a small problem given that the times are formatted as military time (sadly without the leading zero). The code -gen double pcutime = clock(ArrivedOnPCU, "YMDhm")- executes imperfectly.
> After formatting pcutime to %tc, I can see that some of the times translate imperfectly:
> ArrivedOnPCU    pcutime
> 2011/04/06 1630 06apr2011 16:30:00
> 2010/07/18 700  .
> 2011/09/06 400  .
> 2011/06/23 130  23jun2011 13:00:00
> If I manually edit the second obs to read as "2010/07/18 0700" and -replace pcutime = clock(ArrivedOnPCU, "YMDhm"), pcutime displays 18jul2010 07:00:00. It is pretty obvious to me that I'm choosing the wrong mask in the clock function to fail to account for both the missing values in pcutime as well as the incorrect times (i.e. 0130 translating to 13:00).
> I've tried a various permutations of hm/HM/HHMM/hhmm to try to adjust, but to no avail. Can anybody suggest a better mask for me to use? Or perhaps some relatively simple means of inserting a leading "0" into the time portion of the string prior to using -clock-?

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index