Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Days between dates using -forvalues-


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Days between dates using -forvalues-
Date   Mon, 15 May 2006 11:41:24 +0100

Just to add a detail:

You can do arithmetic on the fly in almost
the way that Hugh was trying. 

gen days`i'`=`i'+1' = date`=`i'+1' - date`i' 

would work. However, Jeph's code has the 
considerable benefit of greater clarity. 

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

Hugh Colaco

> Thanks Jeph - it worked great !!!

Jeph Herrin 

> > Try
> >
> >   forvalues i = 1/2 {;
> >       local j=`i'+1
> >       gen days`i'`j' = date`j'-date`i';
> >   };

Hugh Colaco 

> > > I'm trying to get the no of days between dates for 
> several dates. I
> > > want to create variables days12 and days23 (see example below). I
> > > tried to run the foll program
> > >
> > > forvalues i = 1/2 {;
> > > gen days`i'`i+1' = date`i+1'-date`i';
> > > };
> > >
> > > but I get days11 and days22 as the new variables and the 
> values are 0.
> > >

> > > date1     date2       date3            days12         days23
> > > 12/13/1995  5/17/1996 6/14/1996               156    28
> > > 6/22/1999    7/16/1999                     24
> > > 7/3/1996    9/25/1996    10/11/1996             84    16
> > > 3/29/1995    6/23/1995      7/12/1995             86    19
> > > 11/9/1994    5/5/1995      8/17/1995             177    104
> > > 9/22/1998    11/10/1998 11/24/1998    49    14
> > > 9/19/1996    3/25/1997       5/27/1997    187    63
> > > 9/8/1994    10/18/1994        40
> > > 5/13/1993    7/13/1993         8/6/1993    61    24
> > > 6/30/1998    7/23/1998       7/29/1998    23    6
> > > 12/17/1999  1/18/2000   2/7/2000                    32    20
> > >

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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   |   What's new   |   Site index