Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Time format


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Time format
Date   Thu, 12 Oct 2006 12:31:11 +0100

The short answer to your main question is because 
StataCorp have not yet got round to doing it, and this 
is perhaps one of say 100 arguably important things to do 
of which say 20 will be fixed next release. (No, I 
don't know which 20.) 

A secondary answer to your problem is that users often
assume that "time of day" is the name of a single problem, 
but in practice there is really a heap of little problems
depending on all the different inputs and different outputs
that people might have or want. Thus it is more like a single
name applied to a set of problems. 

For those unfamiliar with -ntimeofday- and -stimeofday-, 
these were published in Stata Journal 6(1) 2006, and 
can be downloaded following -search ntimeofday-, 
regardless of whether you subscribe to the Journal. 

But you are right. Time handling in Stata is awkward, but 
what specifically are your problems? 

You cite as a problem

> a failing of the substr command (can't
> specify "all but last two characters")

Note first that -substr()- is a function, not 
a command. Try 

substr("string", 1, length("string") - 2) 

or more usefully 

substr(strvar, 1, length(strvar) - 2) 

The main remaining awkwardness seems to be a need to spell out 
axis labels, which is often less laborious given a loop. See 
for example 

FAQ     . . . . . . . . . . . . . . Getting nice time of day labels on a graph
        9/03    How can I get "nice" time of day labels on a graph?
                http://www.stata.com/support/faqs/graphics/nicetime.html

which includes examples similar to yours. 

Nick 
[email protected] 

Ari Friedman
 
> There was a post recently on the list as to the possibility 
> of creating a percent format
> (http://www.stata.com/statalist/archive/2006-10/msg00208.html), but my
> question is about time.  Specifically, why Stata handles 
> dates so elegantly but time so abysmally.  Once the format is set for a date 
> variable, graphing it produces output exactly as you would expect.  
> Not so for time.
> 
> The data is in the format-
> time	datavar
> 100   ...
> 105   ...
> 155   ...
> 159   ...
> 207   ...
> 1450  ...
> 
> Where 100 is 1:00, 155 is 1:55, and 1450 is 2:50pm.  In searching for
> information on Stata and time, I found the s/ntimeofday 
> functions.  However,
> I don't believe those help much in producing graphs, as the 
> output is a string (they are, however, useful for converting strings to 
> numerical counts of minutes, hours, etc.).  So the question remains: why 
> doesn't Stata handle time in the way it handles dates?
> 
> My best kludge attempt follows.  I am posting it in the hope 
> that someone
> will suggest improvements and/or find it useful, but surely a better
> solution exists?
> 
> *----BEGIN CODE----*
> 
> // Extract minutes
> tostring time, replace
> gen min=substr(time,-2,2)
> 
> // Extract hours
> destring time min, replace
> gen hr = (time - min)/100    
> * Have to do it this way do to a failing of the substr command (can't
> specify "all but last two characters")
> 
> // Now generate a time in minutes
> gen time_min = 60*hr + min
> drop min hr
> 
> // Now feed the time in minutes into the time function
> stimeofday time_min, gen(timestr) string(hours minutes) 
> numeric(minutes)
> 
> // Graph it
> *twoway scatter datavar time_min, xlabel(60 "1:00" 120 "2:00" 
> 180 "3:00" 240
> "4:00" 300 "5:00" 360 "6:00" 420 "7:00" 480 "8:00" 540 "9:00" 
> 600 "10:00"
> 660 "11:00" 720 "12:00" 780 "13:00" 840 "14:00" 900 "15:00" 
> 960 "16:00" 1020
> "17:00" 1080 "18:00" 1140 "19:00" 1200 "20:00" 1260 "21:00" 
> 1320 "22:00"
> 1380 "23:00" 1440 "24:00")
> twoway scatter datavar time_min, xlabel(60 "1:00" 240 "4:00" 
> 420 "7:00" 600
> "10:00" 780 "13:00" 960 "16:00" 1140 "19:00" 1320 "22:00") 
> xtitle("Time of
> Day")
> 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index