title: Replace a subset of data with a sequence of integers
body: <p>I have a dataset that looks like the following:</p>
  
  <pre><code>id    test
  1     A
  2     A
  3     A
  .     B
  .     B
  .     B
  </code></pre>
  
  <p>I would like to fill in the missing values with a sequence of integers corresponding to their <code>id</code>:</p>
  
  <pre><code>id    test
  1     A
  2     A
  3     A
  1     B
  2     B
  3     B
  </code></pre>
  
  <p>I initially thought about using a <code>forvalues</code> loop as follows:</p>
  
  <pre><code>forvalues i=1/3 {
      replace id = `i' if (id == .)
  }
  </code></pre>
  
  <p>But that (for obvious reasons) just replaces all the missing values with <code>1</code> during the first iteration. </p>
  
  <p>I then thought about restricting the loop to a subset of the data by row indexing (like in R), but this functionality doesn't seem to exist for Stata(?): </p>
  
  <pre><code>forvalues i=1/3 {
     replace id[3+`i'] = `i' if (id == .)
  }
  </code></pre>
  
  <p>How can I go about this seemingly simple task in Stata?</p>