Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65050

Loop over files to rename vars to standard naming convention

$
0
0
I'm trying to merge a folder of files, using ID vars: ctry yr
Problem: in each of the using, the naming convention may be
- ctry as ctry country Country Ctry
- yr as yr year Year

Below code is how far I got, thinking the best is to rename each file to standard naming convention first, then do merge.
Below code does not work, because Stata wants to find each of the var names I specify, and stops when it does not find either.
Any suggestions?


Alternatively, is there a way to use merge ID var lists? I.e., to ask stata to 'merge 1:1 (ctry country Country Ctry) yr' instead of 'merge 1:1 ctry yr' or similar?



Code:
*rename vars in using to standard naming convention 
foreach file of local myfilelist{
use `file', clear
foreach x of varlist country Country Ctry{
    ren `x' ctry
}
foreach x of varlist year Year{
    ren `x' yr
}
save, replace
}
*open master data
use "D:\test\CtryList Clipped.dta", clear

*merge all using and save to merged
foreach file of local myfilelist{
merge 1:1 ctry yr using `file'
assert _merge !=2
drop _merge
save merged, replace
}

Viewing all articles
Browse latest Browse all 65050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>