Dear Stata user,
I have an Excel spreadsheet with 13 sheets. I want to import the last 12 sheets into 12 different Stata files and then merge them into a single file. The 12 Excel sheets have exactly the same structure. The name of the Stata variables are made by pasting the name of the original Excel sheet and the original Excel column title (this latter is turned into a label when importing into Stata because it is a numeric value in Excel).
I tried to replicate the code given in the following post http://www.statalist.org/forums/foru...ne-stata-file:
************************************************** **
import excel using "myxlsfile.xls", describe
forvalues sheet=2/`=r(N_worksheet)' {
local sheetname=r(worksheet_`sheet')
import excel using "myxlsfile.xls", sheet("`sheetname'") cellrange(A8:AF154) firstrow case(lower) clear
destring _all,replace
local filename=subinstr("`sheetname'"," ","",.) // remove any space from the Excel sheet names
// This loop is for creating new variable names in Stata combining the original Excel sheet name and Excel column title (imported as label in Stata)
foreach v of varlist b-af {
local newname: var label `v'
rename `v' `filename'`newname'
}
save "`filename'", replace
}
************************************************** **
Initially I had not the option clear after the command "import excel using "myxlsfile.xls"". This works well for the first sheet, but then stops after the first file is saved and return the error message "no; data in memory would be lost".
Alternatively, I have added the option clear after the import excel using "myxlsfile.xls" command as suggested in the post. Again it works for the first sheet but then it stops with the error message "worksheet not found".
Many thanks in advance for any help.
Bertrand
I have an Excel spreadsheet with 13 sheets. I want to import the last 12 sheets into 12 different Stata files and then merge them into a single file. The 12 Excel sheets have exactly the same structure. The name of the Stata variables are made by pasting the name of the original Excel sheet and the original Excel column title (this latter is turned into a label when importing into Stata because it is a numeric value in Excel).
I tried to replicate the code given in the following post http://www.statalist.org/forums/foru...ne-stata-file:
************************************************** **
import excel using "myxlsfile.xls", describe
forvalues sheet=2/`=r(N_worksheet)' {
local sheetname=r(worksheet_`sheet')
import excel using "myxlsfile.xls", sheet("`sheetname'") cellrange(A8:AF154) firstrow case(lower) clear
destring _all,replace
local filename=subinstr("`sheetname'"," ","",.) // remove any space from the Excel sheet names
// This loop is for creating new variable names in Stata combining the original Excel sheet name and Excel column title (imported as label in Stata)
foreach v of varlist b-af {
local newname: var label `v'
rename `v' `filename'`newname'
}
save "`filename'", replace
}
************************************************** **
Initially I had not the option clear after the command "import excel using "myxlsfile.xls"". This works well for the first sheet, but then stops after the first file is saved and return the error message "no; data in memory would be lost".
Alternatively, I have added the option clear after the import excel using "myxlsfile.xls" command as suggested in the post. Again it works for the first sheet but then it stops with the error message "worksheet not found".
Many thanks in advance for any help.
Bertrand
Comment