Statalist


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

Re: st: creating average over past three years within groups


From   "Fabian Brenner" <Fabian.Brenner@gmx.de>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: creating average over past three years within groups
Date   Fri, 03 Oct 2008 16:32:52 +0200

wow! Thank you very very much. And thanks to Martin, as well. Both solutions work. Great help!

Fabian



-------- Original-Nachricht --------
> Datum: Fri, 3 Oct 2008 14:26:36 +0100
> Von: "Eva Poen" <eva.poen@gmail.com>
> An: Statalist <statalist@hsphsun2.harvard.edu>
> Betreff: Re: st: creating average over past three years within groups

> Fabian,
> 
> the error message you get relates to the -local- statements. -local-
> does not allow -if-. A local macro acts like a placeholder and not
> like a variable.
> 
> Take a look at your code:
> 
> local median3 = oneyearmedian if year == `= `y' - 3' & industry == `k',
> 
> First, you don't need the `=`y'-3' construct here, and the comma at
> the end is illegal. More importantly, you ask Stata to perform
> something in one step for which it needs two steps:
> - find out what the median is for industry k three years ago,
> - store this in a local macro.
> 
> You can do it like this:
> 
> sum oneyearmedian if year==`y'-3 & industry == `k'
> local median3 = r(mean)
> 
> 
> However, you could get all of this a lot easier by calculating the
> three year average median for one observation per year and industry,
> and then propagating it along. I assume you have some panel identifier
> called id.
> 
> sort year industry id
> by year industry: gen first = _n==1
> 
> sort first industry year
> by first industry: gen threeyearmean = (oneyearmedian[_n-1] +
> oneyearmedian[_n-2] + oneyearmedian[_n-3])/3 if first==1
> 
> bysort year industry (first) : replace threeyearmean = threeyearmean[_N]
> 
> 
> (Untested.)
> 
> Hope this helps,
> Eva
> 
> 
> 2008/10/3 Fabian Brenner <Fabian.Brenner@gmx.de>:
> > 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
> *
> *   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/

-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index