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
Loop a time-series regression to predict out-of-sample valuesHello there, I am quite new to Stata and after desperately searching for a solution in this forum I …
Fixed EffectIs there any difference in running multicollinearity, autocorrelation and heteroskedasticity test us…
How spatial panel data models deal with endogeneity problem?Hi everyone, I am looking for your suggestion about how to deal with endogeneity bias which comes f…
Non integer weights + summary statistics for population survey dataHi all, I am new to the world of STATA (and fairly new to epidemiology/biostats). I have been taske…
Panel data and autocorrelationDo panel data need autocorrelation? …
Subscribe to:
Post Comments (Atom)
0 Response to Appending Files Conditional on File Name
Post a Comment