Hello, I have several excel files that I need to import, where each sheet is named after the variable it contains data for. This variable name does not appear anywhere else in the sheet. Here is a simplified example of what it looks like:

I finally need to convert this into a panel of the following form:
I now need to extract the sheet name during the import excel process, and store the sheet name as a macro, and then call it when I do reshape long to put it on top of the column. Finally, I do the same for all other sheets in this and other excel files (each excel file has different number of sheets); and combine the files using e.g. joinby.
I don't know how to write the loop that extracts the sheet name from each excel sheet, when the sheet names (i.e. the variable names) are not given beforehand. I came up with this:
Bt I need an idea on the sheet name extraction, and how to import the ith sheet instead of the sheet with name i. Because for example
is giving an error instead of importing the second sheet.
I finally need to convert this into a panel of the following form:
Code:
Country Year GDP Inflation Unemployment Albania 1984 2.1 3.3 0,00 1985 2.2 4.3 0,00 1986 2.3 5.0 0,00 1987 2.4 6.0 1.83 1988 2.6 2.0 0,00 Algeria 1984 1.1 1.0 4,00 1985 1.3 0.8 4,00 1986 1.2 1.5 4,00 1987 1.0 1.3 6,29 1988 1.0 1.0 6,50
I don't know how to write the loop that extracts the sheet name from each excel sheet, when the sheet names (i.e. the variable names) are not given beforehand. I came up with this:
Code:
import excel using $import_file , describe scalar N = r(N_worksheet) *loop through sheets: forval i = 2/`=scalar(N)' { *import sheet i and save as file import excel using $import_file , sheet(i) firstrow clear *somehow extract and save the sheetname and use in reshape command *... save project_file_`i'dta, replace
Code:
import excel using $import_file , sheet(2)
Comment