title: Stata: looping and appending
body: <p>I have a situation where I need to import all the files in a directory and append them. My code is this </p>
  
  <pre><code>local files : dir "C:\Users\xx" files "*.xls"
  
  local n: word count `files'
  tokenize ``files''
  
  cd "C:\Users\xx"
  
  forval  k =1/`n'{
  
  foreach file in `files' {
      import excel "`file'", sheet("Time Sheet") clear
  
  drop in 3
  
  if `k' == 1 {
      di in red `k'
          save "C:\Users\xx\master.dta", replace
      }
      else {
      append using "C:\Users\xx\master.dta"
      }
      save "C:\Users\xx\master.dta", replace
  }
  }
  </code></pre>
  
  <p>However when I use this code it seems to run an extra loop (* forval  k =1/`n'*) that create duplicate entries. I cannot get rid of that code since I need it for the append command. I was wondering whether there is a way to mitigate this problem. </p>