I'm currently performing three separate merges and saving the resulting file of each merge as a temp file. After performing all of these merges, I am appending all three temp files. Here is my current code:
Code:
use `crosswalk', clear
merge 1:1 IDN using `group1'
keep if _merge==3
drop _merge
tempfile crosswalk_1
save `crosswalk_1'
use `crosswalk', clear
merge 1:1 IDN using `group2'
keep if _merge==3
drop _merge
tempfile crosswalk_2
save `crosswalk_2'
use `crosswalk', clear
merge 1:1 IDN using `group3'
keep if _merge==3
drop _merge
tempfile crosswalk_3
save `crosswalk_3'
***************************
******append files*******
***************************
use `crosswalk_1', clear
append using `crosswalk_2'
append using `crosswalk_3'
I realize that it would be more efficient to save group1-group3 as a local and then set a loop. Something like this:
Code:
use `crosswalk', clear
local files `group1' `group2' `group3'
foreach f of local files {
merge 1:1 IDN using `f'
keep if _merge==3
drop _merge
}
What I am not sure how to do is to add a suffix to the loop, so that each resulting file is saved as crosswalk_`suffix'. I thought about creating another local with numbers 1, 2, and 3, but I do not know how to specify that each suffix only be used once, and in order. I believe that correctly specifying this would simplify my code. Advice on how to do this is appreciated. Thanks for reading.
0 Response to Setting a loop for separate merges - add suffix to tempfiles?
Post a Comment