# st: creating average over past three years within groups

 From "Fabian Brenner" To statalist@hsphsun2.harvard.edu Subject st: creating average over past three years within groups Date Fri, 03 Oct 2008 14:17:42 +0200

```Dear all:

I used -bysort year group : egen oneyearmedian = median(age)- to create the median within each group (1-10) for each year (1974-2000).

Now I want to get the three year average of the three median for each group for the most recent three years:

generate threeyearaverage = .
sort year group

quietly forvalues k = 1/10 {
quietly forvalues y = 1974/2000 {
local median3 = oneyearmedian if year == `= `y' - 3' & industry == `k',
local median2 = oneyearmedian if year == `= `y' - 2' & industry == `k',
local median1 = oneyearmedian if year == `= `y' - 1' & industry == `k',
replace fiveyearmedian = (`median3' + `median2' + `median1' ) / 3 if year == `y' & group == `k'
}
}

But stata always says: if not allowed r(101);

What do I wrong? Hope someone can help me.
Fabian Brenner

--
GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
Jetzt dabei sein: http://www.shortview.de/wasistshortview.php?mc=sv_ext_mf@gmx
*
*   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/
```