Statalist The Stata Listserver

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

Re: st: Time format

From   Joseph Coveney <>
To   Statalist <>
Subject   Re: st: Time format
Date   Thu, 12 Oct 2006 15:08:09 +0900

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

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

set more off
input int time_of_day
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'
label values time_of_day Times
list, noobs separator(0) abbreviate(12)

*   For searches and help try:

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