Dear all,
I am asking this question because I am trying to write a program that connects data stored in various directories to a dataset. I suspect that the reason why the program doesn't yet work relates to my (poor) understanding of double compound quotes in Stata.
The program should do something quite simple. The first dataset contains two columns, observation IDs and years. For each observationID-year pair there is other information stored in other files located in a certain directory. I would like to join each observationID-year once for each file in that directory and append everything in a kind of long file. Let me explain you what I mean with an example.
I hope what I am trying to do is clear. If it's not I will provide some more information.
Thank you for your help.
Riccardo
I am asking this question because I am trying to write a program that connects data stored in various directories to a dataset. I suspect that the reason why the program doesn't yet work relates to my (poor) understanding of double compound quotes in Stata.
The program should do something quite simple. The first dataset contains two columns, observation IDs and years. For each observationID-year pair there is other information stored in other files located in a certain directory. I would like to join each observationID-year once for each file in that directory and append everything in a kind of long file. Let me explain you what I mean with an example.
Code:
use dataset.dta // this is my focal dataset
preserve
foreach y of numlist 2006/2012 {
local allfiles`y': dir "G:\folder\"`y'"\folder\" files "*.dta" // I use numbers to reference folders; here is where -set trace- starts getting errors such as 'directory G:\.... not found'
restore, preserve //I take back my dataset and use a chunk of it that corresponds to a certain year
keep if year==`y'
tempfile "portion`y'" // I store the chunk in a tempfile
save `"portion`y'"'
foreach f in `"allfiles`y'"' {
joinby ID year using `f', unmatched(master) // Join the chunk of my dataset to each file in the folder and I append the result
drop _merge
capture confirm file finaljoin.dta
if _rc!=0 {
save finaljoin.dta
}
else {
append using finaljoin.dta
save using finaljoin.dta, replace
clear
use "portion`y'" //I reuse the same chunk of my dataset to join in the next round of the loop to another file located within the same folder
}
}
}
Thank you for your help.
Riccardo

Comment