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: Time between 2 dates
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
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
[email protected]
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/