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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: Re: hours:minutes:seconds |

Date |
Tue, 17 Dec 2002 10:29:38 -0000 |

Gary Longton replied to Christa Scholtz and Radu Ban > > > I have a data field where the duration of an event is > recorded in this > > format: > > > > hour:minute:second > > > > eg: an event that lasts 5 hours, 27 minutes and 13 > seconds is 5:27:13. > > > > How do I get Stata to convert this into total number of seconds? > > and Radu Ban replied: > > > you can try sth like: > > if your_time is the variable you have for time > > > > gen str8 stime = your_time > > gen shours = substr(stime, 1, 2) *takes the first two digits > > gen hours = real(shour) *reads first two digits as number > > gen sminutes = substr(stime, 4, 2) *takes digits 4 and 5 > > gen minutes = real(sminutes) > > gen sseconds = substr(stime, 7, 2) > > gen seconds = real(sseconds) > > > > *now add up > > gen totsecs = 3600*hours + 60*minutes + seconds > > Radu's approach assumes that the original time string will > always have > 2-digit hours, which will often be too restrictive, and > won't work for > Christa's example. > > An easier one-step approach for parsing the time string into the 3 > component numeric variables would be to use Nick Cox's > -split- program > (available on SSC), which could be followed with Radu's > expression for > total seconds. Anyone in this territory might want to know of various -egen- functions in the -egenmore- package on SSC. 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. 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 (baum@bc.edu) is the author of dhms(), elap(), elap2(), hms() and tod(). Nick n.j.cox@durham.ac.uk * * 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/

**References**:**Re: st: Re: hours:minutes:seconds***From:*Gary Longton <glongton@fhcrc.org>

- Prev by Date:
**st: Limin Wang/Person/World Bank is out of the office.** - Next by Date:
**st: MSR** - Previous by thread:
**Re: st: Re: hours:minutes:seconds** - Next by thread:
**Re: st: RE: RE: Chemometrics functions** - Index(es):

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