sonu
sonu

Reputation: 29

How to append files in Stata from the same dir and folder that start with the same letter?

I have a folder called Workdata. In this folder I have the following files.

  1. Mydata_biology
  2. Mydata_chemisty
  3. Mydata_math
  4. uncleandata_1
  5. uncleandata_2

If I wanted to append all the files in this folder I can use the following code :

local allfiles : dir "Data\scores" files "*"

foreach f in local allfiles {
*append loop
} 

However when i try to append only the first 3 that start with Mydata I can no longer use local allfiles

I tried the following code, but it did not work:

local allfiles : dir "Data\scores" files "Mydata*"

foreach f in local allfiles {
*append loop
} 

Upvotes: 1

Views: 6934

Answers (2)

Roberto Ferrer
Roberto Ferrer

Reputation: 11102

The foreach loop is not correctly setup. You want

foreach f of local ... 

and you have

foreach f in local ... 

There's a difference, and it's important. Check help foreach, if needed.

Upvotes: 1

dimitriy
dimitriy

Reputation: 9460

I don't think you need a loop. This is pretty easy with user-written fs:

cd "Data/scores" 
ssc install fs
fs "Mydata*.dta"
append using `r(files)'

Upvotes: 1

Related Questions