[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: RE: calculating elapsed time
Colin A. McKenzie
> I have a data set that has variables time1, date1, time2 and date2. The
> time variables use the 24 hr clock (ie 1:30 pm is written as 13:30) and
> the date variables are in the form dd/mm/yyyy. I need the number of
> hours (to one decimal place) between time1 on date1 and time2 on date2.
> Is there an easy way to calculate this in stata 7?
I presume that -date1- and -date2- are Stata daily
dates and -time1- and -time2- are string variables.
I can't think of a better way than using Stata's
supplied functions, e.g.
gen str1 Index1 = index(time1,":")
gen Time1 =
real(substr(time1, 1, index1 - 1)) / 24 +
real(substr(time1, 1, index1 + 1)) / (24 * 60)
and similar for the other time variable, after
gen diff = 24 * (date2 - date1) + (Time2 - Time1)
* For searches and help try: