# RE: st: Indexing Question

 From n j cox To statalist@hsphsun2.harvard.edu Subject RE: st: Indexing Question Date Tue, 30 Aug 2005 16:09:50 +0100

You won't learn much if you want assistance
every step of the way. Also, the morphing
question ("Ah, but my question is really ...")
strains almost everyone's patience. This is my

Question 1 -----------------------------------------
What if some observations are only in part of that range? suppose i want to calculate the % change in X for all observations from 7/2/05-7/5/05. Since I have a large database, there may be
observations that are in part of the period (e.g. 7/3/05-7/4/05). I want to calculate the % change in X for all observations that appear throughout that period, or a portion of that period.

This is a modification of the previous example. Use -inrange()-,
not -inlist()-.

gen OK = inrange(date, d(2jul2005), d(7jul2005))
bysort id OK (date) :
gen pc_change = 100 * (X[_N] - X[1])/X[1] if OK

Question 2 ---------------------------------------
Also, how can I make it output a single number (for this variable) for each id?

I already gave you a hint on this in pointing to -egen, tag()-.
-tabdisp- is another command that will help.

Nick Cox

>Your dates are to me ambiguous.
>I'll guess your dates are month/day/year.
>
>gen OK = !inlist(date, d(2jul2005), d(7jul2005))
>bysort id (OK date) : gen pc_change = 100 * (X[2] - X[1])/X[1] if OK[1] ==
>0 & OK[2] == 0
>
>The trickery with !inlist() is just a way to make the
>sort order convenient. The OK dates will be coded 0
>and sorted to the first two observations in each panel.
>
>The result will be repeated for each observation in the panel.
>Use -egen, tag()- or some other method to select just one.
>
>Note that your example shows repeated values for
>panel 3.

kelly johnson

> > What if I want to do the same calculation, but in a specific
> > time period.
> > Eg. in my data set, I want to calculate teh percentage change
> > in X between
> > 7/2/05-7/5/05 (for all observations with data in that time
> > period).
*
* 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/