Statalist The Stata Listserver


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

st: Time format


From   "Ari Friedman" <[email protected]>
To   <[email protected]>
Subject   st: Time format
Date   Wed, 11 Oct 2006 17:41:22 -0400

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?

Thanks,
Ari

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

*----END CODE----*


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