# RE: st: egen to calculate industry medians with own frim excluded

 From "Nick Cox" <[email protected]> To <[email protected]> Subject RE: st: egen to calculate industry medians with own frim excluded Date Wed, 19 Dec 2007 20:08:33 -0000

```In addition to Friedrich's helpful answer, note that this is
an FAQ:

FAQ     . . Creating variables recording prop. of the other members of a
group
4/05    How do I create variables summarizing for each
individual properties of the other members of a
group?
http://www.stata.com/support/faqs/data/members.html

The FAQ outlines a method that is likely to be somewhat more
efficient than Friedrich's method, replacing a loop over all
observations with a loop over within-group identifiers.

Nick
[email protected]

Friedrich Huebler
=================

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 == .

Erasmo Giambona
===============

> 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?

*
*   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/
```