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

st: RE: Are there any date with time of day functions?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Are there any date with time of day functions?
Date   Wed, 14 Apr 2004 19:44:22 +0100

The short answer is there is no support for 
time of day functions in official Stata as such. 
There still is much scope for using user-written 
material and/or more general commands or functions.
My best single piece of advice, although it's biased, is 
to look at -split-. 

The material below is extracted from a posting on 28 
January 2003. As far as official or public domain stuff is 
concerned, I think it remains fairly up-to-date. 

=======================

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.

			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. 

[NJC note: now posted at http://www.stata.com/support/faqs/graphics/nicetime.html] 

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

====================

Nick 
[email protected] 

Pauline Rogers
 
> I have just been given a dataset which contains variables 
> which give the
> current date and time i.e. 29.10.1998:15:04.
> 
> I have just searched for any functions that deal with this 
> type of data
> without success (apart from the date functions within Stata).
> 
> I know I can pick out elements from the variables and then 
> manipulate them
> myself but before I re-invent the wheel does anyone know of 
> any functions
> that will help?  I need to extract just the date or just the 
> time; calculate
> intervals between two time points (which could be on 
> different days); etc.
> etc.

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