I have an Excel file (demo.xlsx) with multiple sheets with unique sheet names (A,B,C, etc). I processed something on each and got a "filename.dta" file for each sheet, where "filename" is their Excel's sheet name (A,B,C, etc). Now I want to merge all the .dta together. Here's my code:
The problem is the "local sheetname" is gone after the first loop run, so the second time its run failed. I'd appreciate any help with this. Thanks.
Code:
* I first import the Excel file (demo.xlsx) to get all the sheet names (the sheet names are A, B, C, etc.): import excel using "demo.xlsx", describe * I import the first .dta file as the master file (A.dta) use "~/Desktop/A.dta", clear * I then loop through the rest of the sheets to merge forvalues sheet=1/(`=r(N_worksheet)'-1) { local sheetname=r(worksheet_`sheet') merge 1:1 earliest using "`sheetname'.dta" drop _merge }
Comment