Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: -egenmore- updated -- and a note on time of day


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: -egenmore- updated -- and a note on time of day
Date   Tue, 28 Jan 2003 10:56:05 -0000

Thanks to Kit Baum, the -egenmore- package
on SSC has been updated. This is a miscellany
of -egen- functions by various hands.

The update consists of two functions,
-minutes()- and -seconds()-, to convert
times recorded as strings in the forms
"hh:mm" or "hh:mm:ss" to numeric variables
containing minutes or seconds, respectively.

These updates require Stata 8. The majority
of the -egenmore- functions require Stata 6;
some require Stata 7.

A colleague here asked about what support
there was in Stata for time of day. It is
of course well known that Stata supports
dates at various time scales down to daily
dates, but many projects also include data
specifying time of day. It seems that any
problems which arise lie in the range from

(a) date_and_time problems: that is,
you have a daily date and a time of day
and the two together specify _when_
something happened. Most demanding
is when you want to hold

	daily date
	+ time in hours, minutes, seconds

exactly.

to

(b) time problems: sometimes, the
daily date is irrelevant or secondary
and you just want to focus on time of
day alone.

Some notes follow my signature which may be
of interest. I would appreciate comments
on errors or omissions.

Nick
[email protected]

			Time of day
			===========

This is a note on what I know to be available in Stata to handle time
of
day data.

1. -egen- functions in user-written package -egenmore-
======================================================

Installed (in up-to-date Stata 7 or in Stata 8) by typing

. ssc inst egenmore

or in Stata 6 by using -archinst- (STB-54).

-egenmore- is a library of -egen- functions. The general syntax is
thus
for function fcn

egen [type] newvar = fcn(stuff) [if exp] [in range] [, options]

dhms(d h m s) [ ,  format(format)  ] creates a date variable from
Stata
date variable or date d with a fractional part reflecting the number
of
hours, minutes and seconds past midnight.  h can be a variable
containing integers between 0 and 23 inclusive or a single integer in
that range. m and s can be variables containing integers between 0 and
59 or single integer(s) in that range. Optionally a format, usually
but
not necessarily a date format, can be specified. The resulting
variable,
which is by default stored as a double, may be used in date and time
arithmetic in which the time of day is taken into account.

elap(time) [ ,  format(format)  ] creates a string variable which
contains the number of days, hours, minutes and seconds associated
with
an integer variable containing a number of elapsed seconds. Such a
variable might be the result of date/time arithmetic, where a time
interval between two timestamps has been expressed in terms of elapsed
seconds. Leading zeroes are included in the hours, minutes, and
seconds
fields. Optionally, a format can be specified.

elap2(time1 time2) [ ,  format(format) ] creates a string variable
which
contains the number of days, hours, minutes and seconds associated
with
a pair of time values, expressed as fractional days, where time1 is no
greater than time2. Such time values may be generated by function
dhms(). elap2() expresses the interval between these time values in
readable form. Leading zeroes are included in the hours, minutes, and
seconds fields. Optionally, a format can be specified.

hmm(timevar) generates a string variable showing timevar, interpreted
as
indicating time in minutes, represented as hours and minutes in the
form
"[...h]h:mm". For example, times of 9, 90, 900 and 9000 minutes would
be
represented as "0:09","1:30", "15:00" and "150:00". The option
round(#)
rounds the result: round(1) rounds the time to the nearest minute. The
option trim trims the result of leading zeros and colons, except that
an
isolated 0 is not trimmed. With trim "0:09" is trimmed to "9" and
"0:00"
is trimmed to "0".

hmm() serves equally well for representing times in seconds in
minutes and seconds in the form "[...m]m:ss".

hmmss(timevar) generates a string variable showing timevar,
interpreted
as indicating time in seconds, represented as hours, minutes and
seconds
in the form "[...h:]mm:ss". For example, times of 9, 90, 900 and 9000
seconds would be represented as "00:09","01:30", "15:00" and
"2:30:00".
The option round(#) rounds the result: round(1) rounds the time to the
nearest second. The option trim trims the result of leading zeros and
colons, except that an isolated 0 is not trimmed. With trim "00:09" is
trimmed to "9" and "00:00" is trimmed to "0".

hms(h m s) [ ,  format(format)  ] creates an elapsed time variable
containing the number of seconds past midnight.  h can be a variable
containing integers between 0 and 23 inclusive or a single integer in
that range. m and s can be variables containing integers between 0 and
59 or single integer(s) in that range. Optionally a format can be
specified.

minutes(strvar) [ , maxhour(#) ] returns time in minutes given a
string
variable containing a time in hours and minutes in the form
"[..h]hh:mm". In particular, minutes are given as two digits between
00
and 59 and hours by default are given as two digits between 00 and 23.
The maxhour() option may be used to change the (unreachable) limit:
its
default is 24.                       (Stata 8 required.)

seconds(strvar) [ , maxhour(#) ] returns time in seconds given a
string
variable containing a time in hours, minutes and seconds in the form
"[..h]hh:mm:ss". In particular, minutes and seconds are each given as
two digits between 00 and 59 and hours by default are given as two
digits between 00 and 23. The maxhour() option may be used to change
the
(unreachable) limit: its default is 24.        (Stata 8 required.)

tod(time) [ ,  format(format) ] creates a string variable which
contains
the number of hours, minutes and seconds associated with an integer in
the range 0 to 86399, one less than the number of seconds in a day.
Such
a variable is produced by hms(), which see above. Leading zeroes are
included in the hours, minutes, and seconds fields. Colons are used as
separators.  Optionally a format can be specified.

Kit Baum ([email protected]) is the author of dhms(), elap(), elap2(), hms()
and tod(). The other functions were written by myself. Stata 6
required,
except that minutes() and seconds() require Stata 8.

2. -fodstr-, -fod2str- and -mkfrac-
===================================

Installed (in up-to-date Stata 7 or in Stata 8) by typing

. ssc inst fodstr

N.B. no help files.

fodstr newvar oldvar extracts the fraction-of-a-day stored in Stata's
date format and formats it in readable (hh:mm:ss) form.  fod2str
newvar
oldvar creates the full formatted dd mmmm yyyy hh:mm:ss form. mkfrac
newvar oldvar performs the inverse trasformation; given a string
variable containing a date, it returns the  fraction of a day involved
(i.e. 1 Jan 1998 06:00:00 becomes 0.5000).

William Gould ([email protected]) is the author. Stata 5 required.

3. FAQ on getting nice time labels
==================================

I wrote an FAQ on getting "nice" time labels on graphs which is being
processed by Stata Corp. The advice does not apply in all detail to
Stata 8 graphs.

4. -split-
==========

-split- is official in Stata 8. Otherwise

. ssc inst split

installs an unofficial version requiring Stata 7.

An example at [R] split and in the on-line help explains how to use it
with hh:mm:ss times.

Appendix: Summary of transformation functions
=============================================

egen, dhms()     d h m s           =>      date + fractional part
      elap()     s                 =>      "d:hh:mm:ss"
      elap2()    time1 time2       =>      (time2 - time1) as
"d:hh:mm:ss"
      hmm()      m                 =>      "...h:mm"
                 s                 =>      "...m:ss"
      hmmss()    s                 =>      "...h:mm:ss"
      hms()      h m s             =>      s
      minutes()  "hh:mm"           =>      m
      seconds()  "hh:mm:ss"        =>      s
      tod()      s                 =>      "hh:mm:ss"

fodstr           d with fraction   =>      fraction as "hh:mm:ss"
fod2str          d with fraction   =>      date and time as "d
hh:mm:ss"
mkfrac           "d hh:mm:ss"      =>      d with fraction

NJC 24 January 2003

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