Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# RE: st: RE: calculating elapsed time

 From "Mendoza Aldana, Dr Jorge Antonio (WPRO)" To "statalist@hsphsun2.harvard.edu" Subject RE: st: RE: calculating elapsed time Date Wed, 24 Nov 2010 20:37:04 +0800

```Dear Nick, dear Michael,
Thank you very much for your help, I sincerely appreciate it and your excellent assistance to people like me, through the list. To make a long story short; it happen that I got the data from an external source and while exploring them I notice that a lot of "cleaning" was needed, beside the fact of having dates and times variables in formats I hadn't expected, etc. etc. Now, with calm, I realise that indeed Stata 10 supports date and time fully.
Kind regards,
Jorge

________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox [n.j.cox@durham.ac.uk]
Sent: Wednesday, November 24, 2010 6:52 PM
To: 'statalist@hsphsun2.harvard.edu'
Subject: RE: st: RE: calculating elapsed time

By the way, Stata _10_ also offers full support for date-times.

I was thinking: why isn't Jorge using proper date-times -- and guessing that it was because he is using an outdated version of Stata. But date-time support is also available to him in Stata 10, so the difference is irrelevant here.

Nick
n.j.cox@durham.ac.uk

Michael N. Mitchell

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

* ---------------

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

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
> n.j.cox@durham.ac.uk
>
> 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/

====================================================
This message was scanned for viruses with Trend Micro ScanMail, GFI MailSecurity and  GFI MailEssentials by the World Health Organization Regional Office for the Western Pacific.  However, the recipient is advised to scan this e-mail and any attached files for viruses.

Disclaimer:

This e-mail, together with any attachments, is intended for the named recipients only and is confidential. It may also be privileged or otherwise protected by law.

If you have received it in error, please notify the sender immediately by reply e-mail and delete it and any attachments from your system. You may not copy or disclose its contents to anyone.

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