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
Graphing two categorical variablesHello everyone, I'm new to the Forum and relatively beginner at Stata, so sorry if the answer for w…
Calculating proportions of the make-up of different program groups at different measurement occasions - helpHello, My dataset consists of ~6000 referrals to a suite of four types of psychological programs ov…
Mixed Design ANCOVAHi guys, I am having difficulties with choosing an appropriate statistical analysis for my research…
Integer related issueI'm new to STATA. I used Excel prior but recently purchased STATA. I'm learning as I go. Here is my …
Choice model (cm) data layoutHi all, I am in the midst of setting up my data to run a panel-data mixed logit choice model (cmxtm…
Subscribe to:
Post Comments (Atom)
0 Response to Appending Files Conditional on File Name
Post a Comment