Dear All,
I have the following problem: I need to fill in some particular cells in an Excel sheet, with monthly observations from selected variables in my dataset.
The dataset looks like this:
The Excel sheet will look like this:
I found the following solution (Stata 16.1 version).
I have to fill cell-by-cell a maximum of 396 cells (33 rows x 12 columns) in a single sheet, so Stata opens and saves the Excel file for each cell. There is a second sheet in the same file that needs a similar approach.
I tested this code and it worked perfectly, but I wonder if there was a more efficient solution.
Thank you in advance!
Iulian
I have the following problem: I need to fill in some particular cells in an Excel sheet, with monthly observations from selected variables in my dataset.
The dataset looks like this:
HTML Code:
Year Month aa bb cc dd ee ff ... 2022 1 aa(1) bb(1) cc(1) dd(1) ee(1) ff(1)... 2022 2 aa(2) bb(2) cc(2) dd(2) ee(2) ff(2)... ...... 2023 1 aa(13) bb(13) cc(13) dd(13) ee(13) ff(13)... 2023 2 aa(14) bb(14) cc(14) dd(14) ee(14) ff(14)... ........
The Excel sheet will look like this:
HTML Code:
ColumnToFill RowToFill H K N Q .... 4 aa(13) aa(14) aa(15) aa(16) 6 bb(13) bb(14) bb(15) bb(16) 11 cc(13) cc(14) cc(15) cc(16) 13 dd(13) dd(14) dd(15) dd(16) 18 ee(13) ee(14) ee(15) ee(16) .....
Code:
use mydata ****** create pairs of variables and Excel rows - each variable will pair with a different row in the sheet local var1 "aa bb cc dd ee" // variables to be exported local var2 "4 6 11 13 18" // Excel rows local n : word count `var2' // count no. of pairs (variable, row) ****** loop through each pair forvalues i = 1/`n' { local ob : word `i' of `var1' // select the variable to export local r : word `i' of `var2' // select the row where the variable will be exported to local m=1 // month to start with foreach c in H K N Q T W { // loop through the selected columns of the Excel sheet export excel `ob' using "Analysis.xlsx" if year==2023&month==`m', sheet("BVC", modify) cell(`c'`r') keepcellfmt local ++m // next month } }
I tested this code and it worked perfectly, but I wonder if there was a more efficient solution.
Thank you in advance!
Iulian
Comment