Statalist


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

Re: st: egen to calculate industry medians with own frim excluded


From   "Erasmo Giambona" <[email protected]>
To   [email protected]
Subject   Re: st: egen to calculate industry medians with own frim excluded
Date   Thu, 20 Dec 2007 13:00:16 -0500

Dear Friedrich and Nick,

Thanks very much for you suggestions. This was truly helpful.
Erasmo

On 12/19/07, Friedrich Huebler <[email protected]> wrote:
> Another list member alerted me in a private message that my code gave
> the wrong median value for the first observation. All other
> observations had the correct values.
>
> . clist if group == 1, noobs
>
>     mpg     rep78   foreign      group     median
>      18         1  Domestic          1         21
>      24         1  Domestic          1         18
>
> The median for the first observation should be 24, not 21. The problem
> in my original code is that the sort order changed during the first
> iteration of the loop because of the -bysort group- command. To obtain
> the correct median values for all observations, the -sort- command has
> to be placed before the loop, as in the code below.
>
> sysuse auto, clear
> keep mpg rep78 foreign
> egen group = group(rep78 foreign)
> replace group = . if rep78 == . | foreign == .
> gen median = .
> count
> local n = r(N)
> sort group
> quietly forvalues i = 1/`n' {
>  gen temp = mpg if `i' != _n
>  by group: egen temp2 = median(temp)
>  replace median = temp2 in `i'
>  drop temp temp2
> }
> replace median = . if group == .
>
> The median for the first observation is now 24.
>
> . clist if group == 1, noobs
>
>     mpg     rep78   foreign      group     median
>      18         1  Domestic          1         24
>      24         1  Domestic          1         18
>
> Friedrich
>
> On Dec 19, 2007 11:39 AM, Friedrich Huebler <[email protected]> wrote:
> > Erasmo,
> >
> > The commands below should give you what you need. Other list members
> > may be able to offer more efficient solutions.
> >
> > sysuse auto, clear
> > keep mpg rep78 foreign
> > egen group = group(rep78 foreign)
> > replace group = . if rep78 == . | foreign == .
> > gen median = .
> > count
> > local n = r(N)
> > quietly forvalues i = 1/`n' {
> >   gen temp = mpg if `i' != _n
> >   bysort group: egen temp2 = median(temp)
> >   replace median = temp2 in `i'
> >   drop temp temp2
> > }
> > replace median = . if group == .
> >
> > Friedrich
> >
> >
> > On Dec 19, 2007 6:42 AM, Erasmo Giambona <[email protected]> wrote:
> > > Dear Statalisters,
> > >
> > > I am using the following code to get the median of my ffo variable
> > > within a certain industry/year for a sample of firms:
> > >
> > > egen ffo=median(med_ffoq ), by(industry year).
> > >
> > > However, I need to exclude the own firm before calculating the
> > > industry median for ffo. Does anyone has any suggestions on how I
> > > could do this with egen or any other command?
> > >
> > > Any help would be appreciated.
> > > Regards,
> > > Erasmo
> *
> *   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/
>
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index