title: Stata foreach and forvalue loop
body: <p>Here is my question:
  Repeat the regression in part  1,000 times, drawing new values for the y, X1, X2 and u variables each time as specified. Compute the means of the estimated coefficients and also their correlations.
  I wrote my code with for loop, but it just told me there is nothing changes. </p>
  
  <pre><code>foreach n in 1000{
  
    forvalues m = 1/1000 {
  
          local bb0 = 0
          local bb1 = 0
          local bb2 = 0
          gen u = rnormal(0, 5) if _n &lt;= `n'
          gen x1i = rnormal(0, 1) if _n &lt;= `n'
          gen x2i = exp(x1i) if _n &lt;= `n'
          gen Yi = 2 + 4 * x1i - 6 * x2i + u if _n &lt;= `n'
          regress Yi x1i x2i
  
          replace bb0 = b[_cons]) if _n &lt;= `n'
          replace bb1 = b[x1i] if _n &lt;= `n'
          replace bb2 = b[x2i] if _n &lt;= `n'
  
  }
  }
  gen b0 = mean(bb0)
  gen b1 = mean(bb1)
  gen b2 = mean(bb2)  
  </code></pre>