title: Calculate a difference in stata with if command
body: <p>I want to calculate something like</p>
  
  <pre><code>by group: egen x if y==1 - x if y==2
  </code></pre>
  
  <p>Of course this is not a real stata code but I'm kind of lost. In R this is simply passed by a "[]" behind the variable of intrest but I'm not sure about stata</p>
  
  <p>R would be</p>
  
  <pre><code>x[y==1] - x[y==2]
  </code></pre>