Stata list:
I am trying to create a summary stats table using putdocx that includes the variable names, variable definitions (labels in Stata), observations, mean, sd, max and min and can't’ get the loop to work correctly. When I run the code below, the only variable included in the table is last one (x3 here). All the other values are written over. The resulting table is below.
local variables x1 x2 x3
putdocx table table2 = (3,7), border(all, nil) width(100%) layout(autofitcontents) note(Note: The table notes can be found here.)
putdocx table table2(1,1)=("Summary Statistics"), bold font("Calibri", 12) halign(center) colspan(7) linebreak
putdocx table table2(2,1) = ("Variable")
putdocx table table2(2,2) = ("Variable Definition")
putdocx table table2(2,3) = ("Obs.")
putdocx table table2(2,4) = ("Mean")
putdocx table table2(2,5) = ("Std. Dev.")
putdocx table table2(2,6) = ("Min.")
putdocx table table2(2,7) = ("Max.")
foreach var of varlist `variables' {
local lab: variable label `var'
local nvars: word count `var'
local nrows=`nvars'+2 /* the number of rows in the table is the number of variables + 3: one for the header, one for the footer (notes) and the other for the table title */
quietly summarize `var'
local i=3
while `i'<=`nrows' {
putdocx table table2(`i',1) = ("`var'")
putdocx table table2(`i',2) = ("`lab'")
putdocx table table2(`i',3) = (r(N))
putdocx table table2(`i',4) = (r(mean)), nformat(%5.2f)
putdocx table table2(`i',5) = (r(sd)), nformat(%5.2f)
putdocx table table2(`i',6) = (r(min))
putdocx table table2(`i',7) = (r(max))
putdocx table table2(`i',.), addrows(1, after)
local i=`i'+1
}
}
putdocx save "$mypath\Reports\ExampleReport9-test", replace
exit
This is the resulting table
When I change the addrows line from
putdocx table table2(`i',.), addrows(1, after) to
putdocx table table2(`i',.), addrows(1, before) I do get all of the variables to be listed in the table but they are added from the bottom up (as expected).
This is the resulting table
Can anyone advise on how to add these from top to bottom and get rid of the empty row on top (before the variables)? Thanks.
I am trying to create a summary stats table using putdocx that includes the variable names, variable definitions (labels in Stata), observations, mean, sd, max and min and can't’ get the loop to work correctly. When I run the code below, the only variable included in the table is last one (x3 here). All the other values are written over. The resulting table is below.
local variables x1 x2 x3
putdocx table table2 = (3,7), border(all, nil) width(100%) layout(autofitcontents) note(Note: The table notes can be found here.)
putdocx table table2(1,1)=("Summary Statistics"), bold font("Calibri", 12) halign(center) colspan(7) linebreak
putdocx table table2(2,1) = ("Variable")
putdocx table table2(2,2) = ("Variable Definition")
putdocx table table2(2,3) = ("Obs.")
putdocx table table2(2,4) = ("Mean")
putdocx table table2(2,5) = ("Std. Dev.")
putdocx table table2(2,6) = ("Min.")
putdocx table table2(2,7) = ("Max.")
foreach var of varlist `variables' {
local lab: variable label `var'
local nvars: word count `var'
local nrows=`nvars'+2 /* the number of rows in the table is the number of variables + 3: one for the header, one for the footer (notes) and the other for the table title */
quietly summarize `var'
local i=3
while `i'<=`nrows' {
putdocx table table2(`i',1) = ("`var'")
putdocx table table2(`i',2) = ("`lab'")
putdocx table table2(`i',3) = (r(N))
putdocx table table2(`i',4) = (r(mean)), nformat(%5.2f)
putdocx table table2(`i',5) = (r(sd)), nformat(%5.2f)
putdocx table table2(`i',6) = (r(min))
putdocx table table2(`i',7) = (r(max))
putdocx table table2(`i',.), addrows(1, after)
local i=`i'+1
}
}
putdocx save "$mypath\Reports\ExampleReport9-test", replace
exit
This is the resulting table
Summary Statistics |
||||||
Variable | Variable Definition | Obs. | Mean | Std. Dev. | Min. | Max. |
x3 | This is the variable label for x3 | 980 | 15.51 | 21.38 | 0 | 150 |
Note: The table notes can be found here. |
putdocx table table2(`i',.), addrows(1, after) to
putdocx table table2(`i',.), addrows(1, before) I do get all of the variables to be listed in the table but they are added from the bottom up (as expected).
This is the resulting table
Summary Statistics |
||||||
Variable | Variable Definition | Obs. | Mean | Std. Dev. | Min. | Max. |
x3 | This is the variable label for x3 | 980 | 15.51 | 21.38 | 0 | 150 |
x2 | This is the variable label for x2 | 1357 | 13602.91 | 23750.34 | 0 | 184140 |
x1 | This is the variable label for x1 | 1225 | 132.88 | 242.21 | 0 | 3850 |
Note: The table notes can be found here. |
Can anyone advise on how to add these from top to bottom and get rid of the empty row on top (before the variables)? Thanks.
Comment