I'm trying to build a table using the table command in Stata 17.0. I've set up code to display means and SDs for continuous variables (cont), and N's and %'s for categorical (cat) and binary variables (bin). This code I have works well for outputting continuous variables and their p-values across groups but the formatting is a bit off for categorical variables and binary variables.
As pictured below, the categorical variable header is showing up in two rows, and the p-value is on the first row. I'd like to only have the header appear on one row and the p-value along with it.
For binary variables, I'd like to only see the "success" level, so for this example, only b=1. Currently they're set up the same as the categorical variables.
As a final note, I'm trying to set the code up to be flexible, where if I wanted to add/subtract variables all I'd need to do is add/subtract them from local macros. Thanks for the help!
As pictured below, the categorical variable header is showing up in two rows, and the p-value is on the first row. I'd like to only have the header appear on one row and the p-value along with it.
For binary variables, I'd like to only see the "success" level, so for this example, only b=1. Currently they're set up the same as the categorical variables.
As a final note, I'm trying to set the code up to be flexible, where if I wanted to add/subtract variables all I'd need to do is add/subtract them from local macros. Thanks for the help!
Code:
clear
set more off
collect clear
/* simulating data */
set seed 82
set obs 1000
g female = runiformint(0,1)
label define female 0 "Male" 1 "Female"
label values female female
g cont = female + runiformint(1,7) + runiform() if runiform() > .05
g cat = 1
replace cat = 2 if cont > 2
replace cat = 3 if cont > 5
g bin = cont > 4
/* putting the variables in macros */
local stratvar "female"
local catvars "cat"
local binvars "bin"
local contvars "cont"
table (var) (`stratvar' result), ///
name(stats) ///
statistic(mean `contvars') ///
statistic(sd `contvars') ///
statistic(fvfrequency `catvars' `binvars') ///
statistic(fvpercent `catvars' `binvars')
/* generating p-values */
collect create fits
foreach var in `contvars' `catvars' `binvars' {
collect p = Ftail(e(df_m), e(df_r), e(F)) ///
, tag(var[`var']) ///
: anova `var' `stratvar'
}
/* formatting */
collect layout (var) (result)
collect combine full = stats fits
collect recode result mean = column1 ///
sd = column2 ///
fvfrequency = column1 ///
fvpercent = column2 ///
total = column1 ///
percent = column2
collect layout (var) (`stratvar'#result[column1 column2] result[p])
collect style cell var[`catvars' `binvars']#result[column1], nformat(%6.0fc)
collect style cell var[`catvars' `binvars']#result[column2], nformat(%6.1f) sformat("%s%%")
collect style cell var[`contvars']#result[column1 column2], nformat(%6.1f)
collect style cell var[`contvars']#result[column2], sformat("(%s)")
collect style cell result[p], nformat(%6.3f)
collect label levels result p "p-value", modify
collect style header `stratvar', title(hide)
collect style header result[column1 column2], level(hide)
collect style row stack, nobinder spacer
collect style cell border_block, border(right, pattern(nil))
collect preview
-------------------------------------------------------
Male Female Total p-value
-------------------------------------------------------
cont 4.5 (2.1) 5.5 (2.0) 5.0 (2.1) 0.000
cat 0.000
cat
1 65 13.0% 0 0.0% 65 6.5%
2 208 41.5% 206 41.3% 414 41.4%
3 228 45.5% 293 58.7% 521 52.1%
bin 0.000
bin
0 217 43.3% 138 27.7% 355 35.5%
1 284 56.7% 361 72.3% 645 64.5%
-------------------------------------------------------

Comment