Statalist The Stata Listserver


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

st: RE: Time format


From   "Ari Friedman" <[email protected]>
To   <[email protected]>
Subject   st: RE: Time format
Date   Fri, 13 Oct 2006 12:08:57 -0400

Thanks to both Nick and Joseph for the informative responses.  A particular
thanks is due to Nick for the amazing amount of useful information he posts
every day to the list.  Those of us who post only occasionally but read
daily certainly appreciate it.

My code had a way of rectifying the substr (non-)issue in this specific
case, but Nick's solution is much more general and will be useful in other
things, so thanks for that.  More generally, though, my problems are not the
ones enumerated in the code--those were fixed.  The FAQ Nick linked to,
which I somehow managed to miss in my pre-posting searches, illustrates the
problem quite well.  Graphing time plots is a chore, subject to homebrew
solutions (official or otherwise).  Graphing date plots is simple.  Given
that conceptually they seem very similar (IIRC, dates are stored as days
from a starting point and formatted as the user specifies; in the homebrew
examples, time is treated the same way), I was merely wondering if there
were a better way.  The way that Stata handles dates is so particularly
beautiful (and perhaps those who have worked with dates more than I would
disagree, I do not know) that I was a bit puzzled as to why the same
mechanism was not extended to time.  Both of the examples kindly provided
will work, but I'm sure I am not alone in being left a bit frustrated.

Best,
Ari

------
From: "Nick Cox" <[email protected]>

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] 









Date: Thu, 12 Oct 2006 15:08:09 +0900
From: Joseph Coveney <[email protected]>
Subject: Re: st: Time format

Ari Friedman wrote:

[snip] 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 [snip]

-
----------------------------------------------------------------------------
----

To me, time and date-time display formats are among the "would be nice"
capabilities, but below is a more automated kludge that you can use for
graphing.  You would use it with the -valuelabels- option in the axis
labeling.

First, you'll need to settle on a rational expression of the time variable;
Stata isn't going to know that 100 is one o'clock or 100 minutes from
midnight 1960-01-01 (or is that from midnight 1959-12-31?), or what.

I've used the minutes-from-most-recent-midnight convention in the example
below; most data management packages use finer divisions of time, but for
your purpose, it should be fine.

One of the limitations in using the code below is in the length of string
expressions.  These vary with the context (string variable, string scalar,
macro, value label), and you'll need to keep cognizant of them.

Joseph Coveney

clear
set more off
input int time_of_day
60
65
115
119
127
850
end
*
scalar Times = " "
levelsof time_of_day, local(times)
foreach time of local times {
    scalar time = " `time'" + " ?" + ///
      string(floor(`time'/60)) + ":" + ///
      string(mod(`time', 60), "%02.0f") + "?"
    scalar Times = scalar(Times) + scalar(time)
}
local Times = scalar(Times)
local Times : subinstr local Times "?" `"""', all
local Times label define Times `Times'
`Times'
label values time_of_day Times
list, noobs separator(0) abbreviate(12)
exit


*
*   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