Hi,
I am importing a series of excel docs into stata. Each excel doc has worksheets (two to be exact).
The code I have come up with does not work - and is below. It am trying to generate one variable that records the name of the excel document and another for the name of the sheet. ~~Any assistance would be much appreciated.
cd "~"
local allfiles : dir "." files "*.xls"
tempfile name
save `name', emptyok
foreach f of local allfiles {
import excel using `f', clear
gen source = "`f'"
local n_sheets `r(N_worksheet)'
forvalues j = 1/`n_sheets' {
local sheet`j' `r(worksheet_`j')'
}
forvalues j = 1/`n_sheets' {
gen source1 = `"`sheet`j''"'
append using `name'
save `"`building'"', replace
}
append using `name'
save `name', replace
}
Thanks,
Damian
I am importing a series of excel docs into stata. Each excel doc has worksheets (two to be exact).
The code I have come up with does not work - and is below. It am trying to generate one variable that records the name of the excel document and another for the name of the sheet. ~~Any assistance would be much appreciated.
cd "~"
local allfiles : dir "." files "*.xls"
tempfile name
save `name', emptyok
foreach f of local allfiles {
import excel using `f', clear
gen source = "`f'"
local n_sheets `r(N_worksheet)'
forvalues j = 1/`n_sheets' {
local sheet`j' `r(worksheet_`j')'
}
forvalues j = 1/`n_sheets' {
gen source1 = `"`sheet`j''"'
append using `name'
save `"`building'"', replace
}
append using `name'
save `name', replace
}
Thanks,
Damian
Comment