title: Exporting data files using loop in Stata
body: <p>I have a large dataset of 20 cities and I'd like to split it into smaller ones for each city. Each variable in the dataset will be exported into a text file. </p>
  
  <pre><code>foreach i in Denver Blacksburg {
  use "D:\Data\All\AggregatedCount.dta", clear
  
  drop if MetroArea != `i'
  
  export delimited lnbike using "D:\Data/`"`i'"'/DV/lnbike.txt", delimiter(tab) replace
  export delimited lnped using "D:\Data/`"`i'"'/DV/lnped.txt", delimiter(tab) replace 
  }
  </code></pre>
  
  <p>I tried <code>i' and</code>"`i'"' in the export commands but none of them worked. The error is </p>
  
  <blockquote>
    <p>"Denver not found."</p>
  </blockquote>
  
  <p>I also have cities that have space in between, such as Los Angeles. I tried</p>
  
  <pre><code>local city `" "Blacksburg" "Los Angeles" "Denver" "'
  foreach i of city {
  use "D:\Data\All\AggregatedCount.dta", clear
  
  drop if MetroArea != `i'
  
  export delimited lnbike using "D:/Data/`"`i'"'/DV/lnbike.txt", delimiter(tab) replace
  export delimited lnped using "D:/Data/`"`i'"'/DV/lnped.txt", delimiter(tab) replace 
  }
  </code></pre>
  
  <p>This didn't work either. Do you have any suggestion?</p>