Hello everyone,
I am having trouble with the export excel command and I would really appreciate your help.
I am running a loop through 30 databases and I want to export results to an excel with nine sheets (one for each MAT local), 2 rows (one for each lab local) and 30 columns (one for each database). Nonetheless, everytime I run this code I obtain an excel with nine sheets, two rows but only the cells in the last column have data, the rest of the columns have the variable name but are empty. Any help to try to spot the mistake will be highly appreciated!
PS: First post in the forum, so apologies if any point is not clear enough in my question!
I am having trouble with the export excel command and I would really appreciate your help.
I am running a loop through 30 databases and I want to export results to an excel with nine sheets (one for each MAT local), 2 rows (one for each lab local) and 30 columns (one for each database). Nonetheless, everytime I run this code I obtain an excel with nine sheets, two rows but only the cells in the last column have data, the rest of the columns have the variable name but are empty. Any help to try to spot the mistake will be highly appreciated!
PS: First post in the forum, so apologies if any point is not clear enough in my question!
Code:
local MAT "TOT MEN WOM TOT_M MEN_M WOM_M TOT_N MEN_N WOM_N" foreach x of local MAT{ mat `x'=J(2,30,.) } local lab "works dw" local databases : dir "." files "*2021Q4.csv" foreach db in `databases' { import delimited "`db'", clear local prefix = substr("`db'", 1, 2) local f=1 foreach i of local lab { qui summ `i' [w=weight] if `i' == 1 mat TOT[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & sex==1 mat MEN[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & sex==2 mat WOM[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & nat==0 mat TOT_M[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & sex==1 & nat==0 mat MEN_M[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & sex==2 & nat==0 mat WOM_M[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & nat==1 mat TOT_N[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & sex==1 & nat==1 mat MEN_N[`f',30] = r(sum_w) qui summ `i' [w=weight] if `i' == 1 & sex==2 & nat==1 mat WOM_N[`f',30] = r(sum_w) local ++f } } foreach x of local MAT { drop _all svmat `x' export excel "${folder2}/dsc_stats", sheet("`x'", modify) firstrow(variables) }
Comment