I am trying to create two lists of files and create two new datasets that merges all those files. To do So I was trying the following:
*** SET FOLDER PATHS ***********************************************************
global projectFolder "C:\Users\XXX"
global codeFolder "${projectFolder}\code"
global databaseFolder "${projectFolder}\data"
global rawFolder "${databaseFolder}\raw"
global outputsFolder "${databaseFolder}\output"
*** CREATING VECTORS WITH FILE NAMES *******************************************
global file_all dir "$outputsFolder" files "*.dta"
di `$file_all'
global file_monthly dir "$outputsFolder" files "*_monthly.dta"
di `$file_monthly'
global file_yearly : list global file_all - global file_monthly
di `$file_yearly'
I found a few problems. First, I was not able to create the list of files, and second, I didn't find a way to create this loop without merging twice the first dataset.
*** MERGING YEARLY OUTCOMES ****************************************************
use "$outputsFolder\first_dataset.dta", clear
foreach file in `file_yearly' {
merge 1:1 muni_code year using `file', nogen
}
Within your
foreachloop over the files, you can conditionally load/use if the first file (in the example below, it requires knowing the name of the "first" file), else merge, like this:Output:
Input: