Hi Stata users. I'm using Stata 16.1 on Windows 11 (12 GB RAM), and I've noticed that the putexcel command doesn't reliably transfer the results from Stata commands into Excel spreadsheets when there are many loops and results to write and when the file size is too big (see code below; this code runs for about 24 hours on my computer). I have 33K observations and 4K variables. When I say "reliably transfer the results" I mean that sometimes the data for entire columns are not transferred to the Excel spreadsheets even though the relevant command line is valid and will produce results in the Stata window. In other cases, Stata doesn't transfer the results into some cells in Excel and this appears to be random (as opposed to the situation I noted above in which entire columns are sometimes omitted). Has anyone experienced this before?
foreach impact in TFD_CO2eq TFD_MJ TFD_WSFleq purch_cost {
foreach index in HEI AHEI NRF Med aMed hPDI DASH {
forval quintile=1/5 {
local k=1
foreach food in _tot dairy 101 102 103 104 105 106 ///
protein redmeat 201 202 203 204 205 206 207 plantprot 208 209 211 ///
sandwich 301 302 303 305 ///
soup 401 402 403 soupplant 404 405 406 ///
501 ///
grain grainref 601 602 603 604 605 606 607 608 609 grainwhole 701 702 703 704 705 706 707 708 709 ///
fruit fruitwhole 801 802 803 fruitjuice 804 805 ///
veg 901 902 903 904 905 ///
oil 911 912 913 ///
dessert 921 922 923 924 925 926 ///
bev 931 soda 932 933 935 936 937 938 939 ///
999 {
foreach x in `impact'`food' {
putexcel set "C:\Users\[file path]", sheet ("`impact'_`index'_`quintile'") modify
putexcel A1=("category") B1=("food") C1=("index quintile") D1=("n") E1=("mean") F1=("se") G1=("t") H1=("p") I1=("lb") J1=("ub") K1=("df") L1=("crit") M1=("eform")
local varlabel: var label `x'
local k=`k'+1
capture quietly svy, subpop(if sample==1 & `index'_quant==`quintile'): reg `impact'`food' kcal wave
putexcel D`k'=(e(N_sub))
capture quietly margins, at(kcal=2023) subpop(if sample==1 & `index'_quant==`quintile') vce(unconditional) post
matrix M=r(table)'
capture quietly putexcel E`k'=matrix (M)
capture quietly putexcel A`k'="`impact'" B`k'="`varlabel'" C`k'="`index'_`quintile'"
}
}
}
}
}
foreach impact in TFD_CO2eq TFD_MJ TFD_WSFleq purch_cost {
foreach index in HEI AHEI NRF Med aMed hPDI DASH {
forval quintile=1/5 {
local k=1
foreach food in _tot dairy 101 102 103 104 105 106 ///
protein redmeat 201 202 203 204 205 206 207 plantprot 208 209 211 ///
sandwich 301 302 303 305 ///
soup 401 402 403 soupplant 404 405 406 ///
501 ///
grain grainref 601 602 603 604 605 606 607 608 609 grainwhole 701 702 703 704 705 706 707 708 709 ///
fruit fruitwhole 801 802 803 fruitjuice 804 805 ///
veg 901 902 903 904 905 ///
oil 911 912 913 ///
dessert 921 922 923 924 925 926 ///
bev 931 soda 932 933 935 936 937 938 939 ///
999 {
foreach x in `impact'`food' {
putexcel set "C:\Users\[file path]", sheet ("`impact'_`index'_`quintile'") modify
putexcel A1=("category") B1=("food") C1=("index quintile") D1=("n") E1=("mean") F1=("se") G1=("t") H1=("p") I1=("lb") J1=("ub") K1=("df") L1=("crit") M1=("eform")
local varlabel: var label `x'
local k=`k'+1
capture quietly svy, subpop(if sample==1 & `index'_quant==`quintile'): reg `impact'`food' kcal wave
putexcel D`k'=(e(N_sub))
capture quietly margins, at(kcal=2023) subpop(if sample==1 & `index'_quant==`quintile') vce(unconditional) post
matrix M=r(table)'
capture quietly putexcel E`k'=matrix (M)
capture quietly putexcel A`k'="`impact'" B`k'="`varlabel'" C`k'="`index'_`quintile'"
}
}
}
}
}
Comment