title: Using if qualifier with egen in Stata
body: <p>I'm using Stata, and I'm trying to compute the average price of firms' rivals in a market.  I have data that looks like:</p>
  
  <pre><code>Market    Firm   Price
  ----------------------
  1         1      100
  1         2      150
  1         3      125
  2         1      50
  2         2      100
  2         3      75
  3         1      100
  3         2      200
  3         3      200
  </code></pre>
  
  <p>And I'm trying to compute the average price of each firm's rivals, so I want to generate a new field that is the average values of the other firms in a market.  It would look like:</p>
  
  <pre><code>Market    Firm   Price    AvRivalPrice
  ------------------------------------
  1         1      100      137.2
  1         2      150      112.5
  1         3      125      125
  2         1      50       87.5
  2         2      100      62.5
  2         3      75       75
  3         1      100      200
  3         2      200      150
  3         3      200      150
  </code></pre>
  
  <p>To do the average by group, I could use the egen command:</p>
  
  <pre><code>egen AvPrice = mean(price), by(Market)
  </code></pre>
  
  <p>But that wouldn't exclude the firm's own price in the average, and to the best of my knowledge, using the <code>if</code> qualifier would only change the observations it operated on, not the groups it averaged over.  Is there a simple way to do this, or do I need to create loops and generate each average manually?</p>