Hi all
I will try my best to explain the problem I am facing. I am importing multiple excel files using import excel, no issues here. Each column contains the variable name plus the year, for example Accounts2016. Since I import all the excel files and then append, I do not use the firstrow option as the variable name in the second excel file would be Accounts2015.
The code works perfectly and I then use the autorename function to get the variable names from the firstrow, for example Accounts 2016 (the fact that it is 2016 is not an issue). But this where things get weird. The string in excel is actually "accounts(enter)2016" and not "accounts2016". If I copy paste the variable name from STATA browser I notice it is "Accounts(enter)2016" and the variable becomes useless as I cannot type the enter when writing additional code.
I therefore need one of two solutions. 1) Append the datasets by just stacking them on top of each other (ignore the variable name). This will work as I can the use the firstrow opttion.
2) get rid of the enter in the "string", either in the firstrow before autorenaming or after changing it to the variable name.
Here is the code I have used:
Thank you very much
I will try my best to explain the problem I am facing. I am importing multiple excel files using import excel, no issues here. Each column contains the variable name plus the year, for example Accounts2016. Since I import all the excel files and then append, I do not use the firstrow option as the variable name in the second excel file would be Accounts2015.
The code works perfectly and I then use the autorename function to get the variable names from the firstrow, for example Accounts 2016 (the fact that it is 2016 is not an issue). But this where things get weird. The string in excel is actually "accounts(enter)2016" and not "accounts2016". If I copy paste the variable name from STATA browser I notice it is "Accounts(enter)2016" and the variable becomes useless as I cannot type the enter when writing additional code.
I therefore need one of two solutions. 1) Append the datasets by just stacking them on top of each other (ignore the variable name). This will work as I can the use the firstrow opttion.
2) get rid of the enter in the "string", either in the firstrow before autorenaming or after changing it to the variable name.
Here is the code I have used:
set more off
cd "C:\...."
local allfiles : dir "C:\..." files "*.xlsx"
tempfile building
clear
save `building', emptyok
foreach f of local allfiles {
import excel using "`f'", sheet("Results") case(pre) all clear // You may need to apply certain other options here such
// as -firstrow- or -cellrange()- depending on how these spreadsheets are laid out
gen source = "`f'"
append using `building', force
save `building', replace
}
autorename
cd "C:\...."
local allfiles : dir "C:\..." files "*.xlsx"
tempfile building
clear
save `building', emptyok
foreach f of local allfiles {
import excel using "`f'", sheet("Results") case(pre) all clear // You may need to apply certain other options here such
// as -firstrow- or -cellrange()- depending on how these spreadsheets are laid out
gen source = "`f'"
append using `building', force
save `building', replace
}
autorename
Comment