Hi Everyone,
I've not really tried to use the "newish" table and collect commands to make reproducible tables. Now I have the need I am are not quite getting there hence need some advice.
I want to produce a table where the rows are made up of a number of categorical variables and the columns represent 3 to 4 continuous variables with each cell in the table a summary statistic - mean and SD for example.
Below shows the beginning of such a table with 2 columns showing the the mean and sd of systolic BP according to rows of 3 different categorical variables using a Stata dataset.
I am using Stata version 18.
Resulting Output (abbreviated to show only last part)
So far so good. I now want to add an additional 2 columns for another continuous variable, in the case iron, summarised by the same categorical variables.
Change to code in bold. Ultimately I'll be adding another 4-5 variables.
Which results in the ourput below.
So as you can see the collect layout command fails and I just can't figure out how to edit the last line to get what I want if indeed that is where the issue is.
Ideally I'd like the mean and sd to go together for each variable rather than all the means and then all the SDs.
Many thanks for any advice here.
Kevan
I've not really tried to use the "newish" table and collect commands to make reproducible tables. Now I have the need I am are not quite getting there hence need some advice.
I want to produce a table where the rows are made up of a number of categorical variables and the columns represent 3 to 4 continuous variables with each cell in the table a summary statistic - mean and SD for example.
Below shows the beginning of such a table with 2 columns showing the the mean and sd of systolic BP according to rows of 3 different categorical variables using a Stata dataset.
I am using Stata version 18.
Code:
webuse nhanes2, clear collect clear table sex, statistic(mean bpsystol) /// statistic(sd bpsystol) nototals table race, statistic(mean bpsystol) /// statistic(sd bpsystol) nototals append table hlthstat, statistic(mean bpsystol) /// statistic(sd bpsystol) nototals append collect label levels result count "N" mean "Mean" sd "SD" , modify collect layout (sex race hlthstat) (result)
Code:
collect layout (sex race hlthstat) (result) Collection: Table Rows: sex race hlthstat Columns: result Table 1: 14 x 2 --------------------------------------------- | Mean SD -----------------------+--------------------- Sex | Male | 132.8877 20.99274 Female | 129.0679 25.12684 Race | White | 130.5819 22.81277 Black | 133.7753 26.41772 Other | 128.755 27.4119 Health status | Excellent | 124.3191 19.66825 Very good | 127.3007 21.60186 Good | 132.354 22.88047 Fair | 139.0383 26.0577 Poor | 140.7421 26.06442 Blank but applicable | 130.2143 15.93341 ---------------------------------------------
Change to code in bold. Ultimately I'll be adding another 4-5 variables.
Code:
webuse nhanes2, clear collect clear table sex, statistic(mean bpsystol iron) /// statistic(sd bpsystol iron ) nototals table race, statistic(mean bpsystol iron) /// statistic(sd bpsystol iron) nototals append table hlthstat, statistic(mean bpsystol iron ) /// statistic(sd bpsystol iron ) nototals append collect label levels result count "N" mean "Mean" sd "SD" , modify collect layout (sex race hlthstat) (result)
Code:
. collect clear . table sex, statistic(mean bpsystol iron) /// > statistic(sd bpsystol iron ) nototals --------------------------------------------------------------------------------------------------------- | Mean Standard deviation | Systolic blood pressure Serum iron (mcg/dL) Systolic blood pressure Serum iron (mcg/dL) ---------+----------------------------------------------------------------------------------------------- Sex | Male | 132.8877 103.6798 20.99274 33.82182 Female | 129.0679 95.61792 25.12684 33.86747 --------------------------------------------------------------------------------------------------------- . table race, statistic(mean bpsystol iron) /// > statistic(sd bpsystol iron) nototals append -------------------------------------------------------------------------------------------------------- | Mean Standard deviation | Systolic blood pressure Serum iron (mcg/dL) Systolic blood pressure Serum iron (mcg/dL) --------+----------------------------------------------------------------------------------------------- Race | White | 130.5819 100.2339 22.81277 33.75215 Black | 133.7753 92.17956 26.41772 35.25771 Other | 128.755 103.19 27.4119 37.68521 -------------------------------------------------------------------------------------------------------- . table hlthstat, statistic(mean bpsystol iron ) /// > statistic(sd bpsystol iron ) nototals append ----------------------------------------------------------------------------------------------------------------------- | Mean Standard deviation | Systolic blood pressure Serum iron (mcg/dL) Systolic blood pressure Serum iron (mcg/dL) -----------------------+----------------------------------------------------------------------------------------------- Health status | Excellent | 124.3191 102.7848 19.66825 34.37272 Very good | 127.3007 101.6129 21.60186 34.83534 Good | 132.354 99.19571 22.88047 33.4414 Fair | 139.0383 95.03533 26.0577 33.11717 Poor | 140.7421 91.92318 26.06442 32.8804 Blank but applicable | 130.2143 95.14286 15.93341 37.67239 ----------------------------------------------------------------------------------------------------------------------- . . collect label levels result count "N" mean "Mean" sd "SD" , modify . . collect layout (sex race hlthstat) (result) Collection: Table Rows: sex race hlthstat Columns: result Your layout specification does not uniquely match any items. One or more of the following dimensions might help uniquely match items: cmdset, colname, statcmd, var.
Ideally I'd like the mean and sd to go together for each variable rather than all the means and then all the SDs.
Many thanks for any advice here.
Kevan
Comment