title: Using Stata's keep command on multiple blocks of variables
body: <p>I just started working on a massive dataset with 5 million observations and lots and lots of variables. To process this faster, I want to select only some variables of interest and drop the rest. </p>
  
  <p>with <code>keep</code>, I could select a block of variables, very simple:</p>
  
  <pre><code>keep varx1-x5 
  </code></pre>
  
  <p>However, the variables I want are not in order in the dataset:</p>
  
  <pre><code>varx1 varx2 varx3 varz1 varz2 vary1 vary2 vary3
  </code></pre>
  
  <p>Where I don't want the <code>varz</code> variables. I want only the blocks with <code>varx</code> and <code>vary</code>.</p>
  
  <p>So. I'm not very good at loops, but I tried this:</p>
  
  <pre><code>foreach varname of varlist varx1-varx3 vary1-vary3  {
  keep `varname'
  }
  </code></pre>
  
  <p>This doesn't work,  because it <code>keep</code>s only <code>varx1</code>, then tries to <code>keep</code> the others, and errors out because they have just been <code>drop</code>ped. </p>
  
  <p>How can I tell <code>keep</code> to select multiple blocks of variables?</p>