Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

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


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: problem using -clock- with military time
Date   Sat, 2 Jun 2012 00:00:31 +0100

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

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
problems.

for a review.

On Fri, Jun 1, 2012 at 11:39 PM, Steve Nakoneshny <scnakone@ucalgary.ca> 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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index