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: Days between
From
David Kantor <[email protected]>
To
[email protected]
Subject
Re: st: Days between
Date
Wed, 09 Jan 2013 09:45:32 -0500
At 09:20 AM 1/9/2013, Damian wrote:
I have two date formats which I have imported from Excel. The first
is a in dd/mm/yyyy hh:mm format (e.g. 22/04/2011 12:08). The second
is dd/mm/yy (e.g. 22/08/2012). Both dates are currently stored as
float and formatted in %tc.
I would like to calculate the days between these two dates. I was
able to when both dates were in dd/mm/yy. However, with the inclusion
of hh:mm I am no longer able to.
How would I calculate the days between?
Presumably, the variable in what you described as dd/mm/yyyy hh:mm
format actually
has a %tc format. It does not make sense that the other is %tc. Is it
really %td ??
You need to generate a new variable that has a %td format and which
ignores the hh:mm part of the original values. Use the dofc function.
gen long newvar = dofc(origvar)
format newvar %td
Now you can compare newvar to the other date variable -- that is,
take the difference.
(You could do this as a -replace- if you don't mind losing information.
You could also skip the replace or generate:
gen int datediff = date1 - dofc(date2)
-- assuming that date2 is %tc and date1 is %td, or...
gen int datediff = dofc(date1) - dofc(date2)
if they are both %tc.
)
HTH
--David
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/