Hello Statalist Members,
Hope I can get some help, been trying to no avail.
Problem:
1. Need to merge 10 dta files (fileOne.dta, fileTwo.dta, ... fileTen.dta) to become 1 file
2. Keeping the resulting _merge, renaming _merge to include merged file names, and then dummying the renamed _merge variable--to generate 3 additional dummy variables.
the first merge 1:1, merges fileone.dta and filetwo.dta
_merge will be renamed: _mergeOneTwo
3 dummy variables: _mergeOneTwo1 (value is 1 if _merge==1), _mergeOneTwo2 (value is 1 if _merge==2), _mergeOneTwo3 (value is 1 if _merge==3)
the second merge 1:1,merges the result of the first merge with filethree.dta
the resulting _merge variable will need to be renamed _mergeOneTwoThree
3 more dummy variables _mergeOneTwoThree1 (is 1 if _merge==1), _mergeOneTwoThree2 (is 1 if _merge==2), _mergeOneTwoThree3 (is 1 if _merge==3)
and so on...
Questions:
1. How to rename _merge to include master & using file names within a loop ?
2. How to dummy resulting renamed _merge within a loop?
What I tried (and failed):
the variable for matching among files: CheeseBrand
Error message/s encountered:
variable _MC not found
r(111);
end of do-file
r(111);
.
I welcome any correction/ suggestion or redirection to accomplish this. Thank you
Hope I can get some help, been trying to no avail.
Problem:
1. Need to merge 10 dta files (fileOne.dta, fileTwo.dta, ... fileTen.dta) to become 1 file
2. Keeping the resulting _merge, renaming _merge to include merged file names, and then dummying the renamed _merge variable--to generate 3 additional dummy variables.
the first merge 1:1, merges fileone.dta and filetwo.dta
_merge will be renamed: _mergeOneTwo
3 dummy variables: _mergeOneTwo1 (value is 1 if _merge==1), _mergeOneTwo2 (value is 1 if _merge==2), _mergeOneTwo3 (value is 1 if _merge==3)
the second merge 1:1,merges the result of the first merge with filethree.dta
the resulting _merge variable will need to be renamed _mergeOneTwoThree
3 more dummy variables _mergeOneTwoThree1 (is 1 if _merge==1), _mergeOneTwoThree2 (is 1 if _merge==2), _mergeOneTwoThree3 (is 1 if _merge==3)
and so on...
Questions:
1. How to rename _merge to include master & using file names within a loop ?
2. How to dummy resulting renamed _merge within a loop?
What I tried (and failed):
the variable for matching among files: CheeseBrand
Code:
cd "C:\Users\HP\Desktop\doFileDraft" fs *.dta local filenames: dir "." files "*.dta" local first_file: word 1 of `filenames' local filenames: list filenames - first_file local fish : subinstr local file ".dta" "" use "`first_file'", clear tempfile building save `building' foreach f of local filenames { use `f', clear merge 1:1 CheeseBrand using `building' rename _merge _M`fish' tabulate _M`building'`fish', generate (_M`building'`fish') save `"`building'"', replace }
variable _MC not found
r(111);
end of do-file
r(111);
.
I welcome any correction/ suggestion or redirection to accomplish this. Thank you
Comment