Hi all,
I am relatively new to Stata and am somewhat stuck on an data visualisation issue. I am trying to make a descriptive table with colums of Total (column percentage) and category levels (row percentage), and rows of variables (and variable categories), but I cannot figure out how to combine those two (I either get one or the other in the output).
The code is something like this, using examples from previopus forum posts (this is not complete and I put *** where I tried to merge the two tables from the for loops):
The output would be something like this (this was manually done):

Thank you!
I am relatively new to Stata and am somewhat stuck on an data visualisation issue. I am trying to make a descriptive table with colums of Total (column percentage) and category levels (row percentage), and rows of variables (and variable categories), but I cannot figure out how to combine those two (I either get one or the other in the output).
The code is something like this, using examples from previopus forum posts (this is not complete and I put *** where I tried to merge the two tables from the for loops):
Code:
webuse nhanes2l unab vlist : smsa sex race hlthstat heartatk diabetes highlead * build the overall sample table table () (rural), /// stat(frequency) /// stat(percent, across(rural)) /// name(N) collect addtags N[_hide] collect layout (N) (rural#result) *loop 1 — generate (column percent) foreach v1 of local vlist { table (`v'), /// stat(frequency) /// stat(percent) /// totals(`v') /// name(tot_`v') } * loop 2 — generate row percent and p-value foreach v2 of local vlist { table (`v') (rural), /// stat(frequency) /// stat(percent, across(rural)) /// nototals /// name(`v') * p-value quietly tabulate `v' rural if in_model_sample == 1, chi2 collect get p = (r(p)), name(`v') tags(`v'[1] rural[Test]) } *** * table format collect composite define stats = frequency percent collect style cell result[percent], nformat("%6.2f") sformat("(%s%%)") collect style cell result[p], nformat("%5.3f") minimum(.001) collect style header result[stats p], title(hide) level(hide) * Test to the end collect levels rural local levels = s(levels) local levels : subinstr local levels "Test" "" collect style autolevels rural `levels' Test, clear * layout collect layout (N `vlist') (rural#result[stats])
Thank you!
Comment