I have a number of files that are named the following:

lp1_x0_rf1.dta
...
lp1_x9_rf1.dta
lp1_x0_rf2.dta
...
lp1_x9_rf2.dta
...
lp1_x9_rf9.dta
lp2_x0_rf1.dta
...
lp2_x9_rf1.dta
lp2_x0_rf2.dta
...
lp2_x9_rf2.dta
...
lp2_x9_rf9.dta

Just in case the naming convention is not clear, they all begin with lp and then 1 or 2, followed by x and then 0 through 9. Finally is rf and then 0 through 9.

I would like to append lp1_x0_rf1.dta to lp2_x0_rf1.dta and save it as a new file. I am struggling to write a loop that automates this, appending each lp1 file to each lp2 file conditional on them having the same x and rf values. As of now, I have a loop that appends every lp2* file to each lp1* file.

loc fileList1 : dir "`root'" files "lp1*"
foreach file1 in `fileList1' {
use "`root'/`file1'", clear

loc fileList2 : dir "`root'" files "lp2*"
foreach file2 in `fileList2' {
append using "`root'/`file2'", force
}

loc ext = substr("`file1'", 5, 2)
loc sat = substr("`file1'", 8, 3)
save "`export'/lp_`ext'_`sat'.dta", replace
}

If I could use the -if- qualifier with the -append- command, I feel like the problem would be easy. As it is, I cannot figure out how to get Stata to examine the components of two file names and then append them if those components are equal to each other.

Thanks for any suggestions!