title: Generate new variable using min/max in Stata
body: <p>After many years away from Stata I am currently editing code which repeatedly does something like this:</p>
  
  <pre><code>egen min = min(x)
  egen max = max(x)
  generate xn = (x - min) / (max - min)
  drop min max
  </code></pre>
  
  <p>I want to reduce this code to one line. But neither of the two "natural" ways that come to my mind work. </p>
  
  <pre><code>gen x_index = (x - min(x)) / (max(x)- min(x))
  egen x_index = (x - min(x)) / (max(x)- min(x))
  </code></pre>
  
  <p>What pieces of the Stata logic am I missing? </p>