Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Ariel Linden, DrPH" <ariel.linden@gmail.com> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | re: Re: st: generating a seq() going backwards and forward from a given timepoint? |
Date | Fri, 14 Dec 2012 12:51:05 -0500 |
Wow, Nick, this is perfect!!!! Brilliant as always! Thank you, kind sir. Ariel From Nick Cox <njcoxstata@gmail.com> To statalist@hsphsun2.harvard.edu Subject Re: st: generating a seq() going backwards and forward from a given timepoint? Date Fri, 14 Dec 2012 12:00:04 +0000 ________________________________________ Looking again at Ariel's question, he does want sequence numbers and not dates shifted to a new origin, so my last part does not apply. In that case, I would recommend a three step. bys ID (date): egen period = seq() if date >= mdy(1,1,2012) by ID : egen nmissing = total(missing(period)) by ID : replace period = -nmissing + _n - 1 if missing(period) Nick On Fri, Dec 14, 2012 at 9:59 AM, Nick Cox <njcoxstata@gmail.com> wrote: > -seq()- as an -egen- function is part of official Stata but it has roots in > > STB-50 dm70 . . . . . . . . . . . . . . . . Extensions to generate, extended > (help egenodd if installed) . . . . . . . . . . . . . . . . N. J. Cox > 7/99 pp.9--17; STB Reprints Vol 9, pp.34--45 > 24 additional egen functions presented; includes various string, > data management, and statistical functions; > many of the egen functions added to Stata 7 > > STB-37 dm44 . . . . . . . . . . . . . . . . . . . . . Sequences of integers > (help seq if installed) . . . . . . . . . . . . . . . . . . N. J. Cox > 5/97 pp.2--4; STB Reprints Vol 7, pp.32--33 > egen now has seq() function > > so its generality and flexibility are a test of my work. (Bizarrely a > fragmentary reference to -seq- survives in the manual entry for > -egen-, but that is scheduled for removal.) > > You can specify a negative argument to -from()-, but Ariel wants here > is a starting value that may differ between panels. -from()- must be > supplied with an integer argument, and although you can set it up that > it sees the result of an expression that evaluates to an integer, that > result must be a constant, which as in this problem is likely to be > too inflexible for panels. > > So, there is a case for generalising -seq()- to accept more general > expressions, or possibly for adding an -origin()- option. My own > suggestion is that the latter would spoil the simplicity of -seq()- > > That said, I would just use > > gen period = date - mdy(1,1,2012) + 1 > > here and I suspect that whenever people have wanted something like > that it has seemed more natural to do it directly than to fire up > -egen-. > > -seq()- is a fairly trivial function. It is arguable that its main > feature is to support blocks of equal identifiers, which can be useful > e.g. for ANOVA problems. My recollection is that its origins lie in > wanting to use it and in reading about similar functionality in what > was then just described as S. > > Nick > > On Fri, Dec 14, 2012 at 3:31 AM, Ariel Linden, DrPH > <ariel.linden@gmail.com> wrote: > >> This is probably a very simple solution, but I am getting stuck. I'd like to >> generate a running sequence going both backwards and forwards from a >> specified timepoint. -seq()- is pretty straightforward (pun intended) for >> going forward, but I can't seem to figure out the backwards part. >> >> Below I have and data with the "period" variable that I'd like to achieve >> via code (I did this manually). For this example, I specified that >> 01/01/2012 is the starting point and ran the following code: >> >> . display date("01/01/2012", "MDY") >> 18993 >> >> . bys ID: egen period = seq() if date>=18993 >> (48 missing values generated) >> >> How do I get the backward periods (-1 to -24)? There will be IDs with >> varying numbers of periods available, so this must work via bysort ID, >> anchored on the same starting date for everyone: >> >> ID date period >> WC9 01/01/2010 -24 >> WC9 02/01/2010 -23 >> WC9 03/01/2010 -22 >> WC9 04/01/2010 -21 >> WC9 05/01/2010 -20 >> WC9 06/01/2010 -19 >> WC9 07/01/2010 -18 >> WC9 08/01/2010 -17 >> WC9 09/01/2010 -16 >> WC9 10/01/2010 -15 >> WC9 11/01/2010 -14 >> WC9 12/01/2010 -13 >> WC9 01/01/2011 -12 >> WC9 02/01/2011 -11 >> WC9 03/01/2011 -10 >> WC9 04/01/2011 -9 >> WC9 05/01/2011 -8 >> WC9 06/01/2011 -7 >> WC9 07/01/2011 -6 >> WC9 08/01/2011 -5 >> WC9 09/01/2011 -4 >> WC9 10/01/2011 -3 >> WC9 11/01/2011 -2 >> WC9 12/01/2011 -1 >> WC9 01/01/2012 1 >> WC9 02/01/2012 2 >> WC9 03/01/2012 3 >> WC9 04/01/2012 4 >> WC9 05/01/2012 5 >> WC9 06/01/2012 6 >> WC9 07/01/2012 7 >> WC9 08/01/2012 8 * * 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/