Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: data management - converting string HH:MM to actual numeric integer values


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: data management - converting string HH:MM to actual numeric integer values
Date   Thu, 29 Jan 2009 19:39:11 -0000

Stata 8 has no official idea of time (of day), only date. For that you
need to upgrade to Stata 10. 

There is a relevant example in [R] split in the Stata 8 manuals. From it
you can deduce 

split duration, p(:) destring 
gen tminutes = 60 * duration1 + duration2 

Alternatively, you can go straight there with 

gen tminutes = 
60 * real(substr(duration, 1, 2)) + real(substr(duration, 4, .)) 

However, there may be leading or trailing spaces if that is a str8. At
first sight str5 would suffice. -trim()- it first. Or perhaps these
durations can be longer than a day. In which case 

gen colon = index(duration, ":") 

gen tminutes = 60 * real(substr(duration,1,colon-1)) +
real(substr(duration, colon+1, .)) 

In other words, the -split- method is more general. 

Nick 
[email protected] 

b. water
 
Stata 8.2,
 
I really must apologize for asking this query. I am on the road for a
few days and do not have access to my Stata manuals. Much to my
annoyance I discovered that I have not dealt with one of the variable
(called duration) in the form of hh:mm e.g. 01:55 i.e. duration of an
event that lasted an hour and fifty five minutes. I am sure this would
have been described somewhere in the manuals.
 
As it stands, the variable is thus described: str8 %9s.
 
I tried to look at the stata on-line help on date and time and something
i thought would be good i.e. str2time to see how I can convert this to
time duration that I can subsequently analyze (e.g. to be able summarize
or create categories) but without success. 
 
I would therefore be grateful for any advice/help on how to convert this
string variable to a form or format that stata can recognise as time
hh:mm or better still to just convert them to integers e.g. 01:55 would
then become 115 (minutes) - an integer. Of course any other suggestions
would be valued.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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