Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: RE: calculating elapsed time
From 
 
"Michael N. Mitchell" <[email protected]> 
To 
 
[email protected] 
Subject 
 
Re: st: RE: calculating elapsed time 
Date 
 
Wed, 24 Nov 2010 02:43:12 -0800 
Greetings
   I just finished typing up my version of the answer when I saw Nick's post... It is 
just another way about it (and some might think a more roundabout way). It borrows from an 
example from my book. Steps 1 to 4 involve converting my book example into one matching 
the reader's question
* ---------------
. * Step 1. get data from book
. * net from http://www.stata-press.com/data/dmus/
. * net get dmus1
. * net get dmus2
.
. * Step 2. read in data
. clear
. insheet using momkid1a.csv
(8 vars, 4 obs)
.
. * Step 3. create date1 hh1 mm1
. gen date1 = mdy( momm, momd, momy)
. format date1 %td
. rename momh hh1
. rename mommin mm1
. list date1 hh1 mm1
     +-----------------------+
     |     date1   hh1   mm1 |
     |-----------------------|
  1. | 28nov1972    10    38 |
  2. | 03apr1973     6    22 |
  3. | 13jun1968    22    45 |
  4. | 05jan1960    15     1 |
     +-----------------------+
.
. * Step 4. create date2 hh2 mm2
. gen double clock2 = clock(kidbday,"MDYhms")
. gen hh2 = hh(clock2)
. gen mm2 = mm(clock2)
. gen date2 = dofc(clock2)
. format date2 %td
.
. order date1 hh1 mm1 date2 hh2 mm2
. keep date1 hh1 mm1 date2 hh2 mm2
* ---------------
  And then this example answers the question...
.
. * Now answer the question.
.
. * Step 5) make datetime for #1
. generate double datetime1 = dhms(date1,hh1,mm1,0)
. format datetime1 %tc
. list  date1 hh1 mm1 datetime1
     +--------------------------------------------+
     |     date1   hh1   mm1            datetime1 |
     |--------------------------------------------|
  1. | 28nov1972    10    38   28nov1972 10:38:00 |
  2. | 03apr1973     6    22   03apr1973 06:22:00 |
  3. | 13jun1968    22    45   13jun1968 22:45:00 |
  4. | 05jan1960    15     1   05jan1960 15:01:00 |
     +--------------------------------------------+
.
. * Step 6) make datetime for #2
. generate double datetime2 = dhms(date2,hh2,mm2,0)
. format datetime2 %tc
. list  date2 hh2 mm2 datetime2
     +--------------------------------------------+
     |     date2   hh2   mm2            datetime2 |
     |--------------------------------------------|
  1. | 05jan1998    15    21   05jan1998 15:21:00 |
  2. | 11apr2002    10    49   11apr2002 10:49:00 |
  3. | 15may1996     1    58   15may1996 01:58:00 |
  4. | 04jan2004    23     1   04jan2004 23:01:00 |
     +--------------------------------------------+
.
. * Step 7) compute difference in milliseconds
. generate double diffms = datetime2 - datetime1
. list datetime1 datetime2 diffms
     +-----------------------------------------------------+
     |          datetime1            datetime2      diffms |
     |-----------------------------------------------------|
  1. | 28nov1972 10:38:00   05jan1998 15:21:00   7.922e+11 |
  2. | 03apr1973 06:22:00   11apr2002 10:49:00   9.159e+11 |
  3. | 13jun1968 22:45:00   15may1996 01:58:00   8.810e+11 |
  4. | 05jan1960 15:01:00   04jan2004 23:01:00   1.388e+12 |
     +-----------------------------------------------------+
.
. * Step 8) compute difference in days
. generate diffdays = diffms / (1000*60*60*24)
. list datetime1 datetime2 diffms diffdays
     +----------------------------------------------------------------+
     |          datetime1            datetime2      diffms   diffdays |
     |----------------------------------------------------------------|
  1. | 28nov1972 10:38:00   05jan1998 15:21:00   7.922e+11   9169.196 |
  2. | 03apr1973 06:22:00   11apr2002 10:49:00   9.159e+11   10600.19 |
  3. | 13jun1968 22:45:00   15may1996 01:58:00   8.810e+11   10197.13 |
  4. | 05jan1960 15:01:00   04jan2004 23:01:00   1.388e+12   16070.33 |
     +----------------------------------------------------------------+
I hope this helps,
Michael N. Mitchell
Data Management Using Stata      - http://www.stata.com/bookstore/dmus.html
A Visual Guide to Stata Graphics - http://www.stata.com/bookstore/vgsg.html
Stata tidbit of the week         - http://www.MichaelNormanMitchell.com
On 2010-11-24 2.35 AM, Nick Cox wrote:
You need to convert the strings to numbers and do the arithmetic. It's as basic as that.
Assuming that your -date1- and -date2- are Stata daily dates
gen elapsed_time =
24 * (date2 - date1)
+ real(HH2) - real(HH1)
+ (real(MM2) - real(MM1)) / 60
That's one command, just spaced for legibility.
Stata 11 offers full support for date-times.
Nick
[email protected]
Mendoza Aldana, Dr Jorge Antonio
I am using Stata 10 and I would like to calculate the elapsed time, in hours, between two time points. My data is formatted as below, the hours and minutes are separate string variables.
variable: date1  HH1  MM1  date2  HH2  MM2
           -----  ---  ---  -----  ---  ---
format:   date   str  str  date   str  str
I would appreciate very much your assistance in finding a way to solve this or indicating me any related link.
*
*   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/
*
*   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/