Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: Time between 2 dates


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Time between 2 dates
Date   Wed, 26 Jan 2011 17:42:09 +0000

Notice that you can omit the intermediate variables if you don't want them. 

gen long daysbetween = date(enddate, "YMD") - date(startdate, "YMD")

As David explained, this is based on the assumption that you have string variables. If you have numeric variables with date formats, then the time between two dates is just the difference. 

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

David Kantor

At 12:06 PM 1/26/2011, Laurie wrote:
>Hi all, i would like to calculate the time between 2 dates in a data set.
>
>I have start date like: 2010/08/02 meaning august 2nd 2010
>and end date like: 2011/02/08 meaming february 2nd 2011.
>
>I would like to know how many days are between the start date and 
>the end date.
>[...]

Presumably these values such as 2010/08/02 are of string types. First 
convert them to Stata dates. Assuming the variables are named 
startdate and enddate:
gen long startdate_n = date(startdate, "YMD")
gen long enddate_n = date(enddate, "YMD")

(The new variables are named with _n to signify "numeric", but of 
course, the names are arbitrary.)
You may also want...
format startdate_n enddate_n %d
(or any of several date formats) but that is optional.
Then just take the difference:
gen long daysbetween = enddate_n -startdate_n


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index