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!
Related Posts with Appending Files Conditional on File Name
Newbie: run file twice in a rowHello everybody, first post and very new to Stata. I'm doing a university course on it and one requi…
Postestimation using first-stage coefficients in 2SLSDear Stata experts, I would like to do some postestimation analysis of an IV regression using coeff…
Panel data analysisDear everyone, I hope you are all doing well. I am a beginner in panel data analysis and Stata. My …
not readable outcomesHi every one kindly why my stata program suddenly comes out the tables of results like the following…
Formatting confidence intervals in .csv using esttabHi All, This is an issue I've had when using esttab for several years that I've never been able to f…
Subscribe to:
Post Comments (Atom)
0 Response to Appending Files Conditional on File Name
Post a Comment