Warm tip: This article is reproduced from serverfault.com, please click

Merge all .dta files in one folder?

发布于 2020-12-04 23:02:39

I have a folder with 36 .dta files which are all structured the same. Each one has 2 fields: RowID and value. Each file also has the same number of rows (2,500). The name of the "value" variable is unique to each file. I would like to construct a loop that loads in the first .dta file and then merges the "value" variable from each of the other 35 files. Any help will be greatly appreciated.

Here are sample data from 3 of the .dta files:

Example 1:
input int rowid_ float value_ex_1
 1 0
 2 0
 3 0
 4 1
 5 1
 6 1
 7 1
 8 1
 9 1
10 1

Example 2:
input int rowid_ float value_ex_2
 1 1
 2 0
 3 0
 4 1
 5 1
 6 0
 7 0
 8 0
 9 0
10 0

Example 3:
input int rowid_ float value_ex_3
 1 0
 2 0
 3 0
 4 0
 5 0
 6 1
 7 1
 8 0
 9 0
10 1


Questioner
Nick M.
Viewed
0
krasnapolsky 2020-12-06 20:05:37

In order to loop over all your .dta files, first make sure they are named after a logical order (i.e example_1.dta, example_2.dta, example_3.dta etc).

Then, you can load the first dataset and loop over the other ones with a forvalues loop:

cd "path/to/your/datasets"

use example_1.dta, clear

forvalues i = 2(1)35 { 
    merge 1:1 rowid_ using example_`i'.dta
    drop _merge
}