I am running into two problems:
1. I cannot use a binary variable for counting in the table. It seems to have to be a float, e.g. blood pressure or BMI
I tried to find a solution in "STATA CUSTOMIZABLE TABLES AND COLLECTED RESULTS REFERENCE MANUAL RELEASE 18", but with no luck. .
2. I cannot control number of decimals in percentages and add the "%" to that column estimates
Something is not working with collect and the formatting of cells. I am working on version 19.5 with all updates implemented.
I can do the same with a combination of collapse or simple counting and creating manual arrays with putdocx, but creating a targeted collection seems the optimal strategy from now onwards. I worked with Stata since about version 5 and have written many ado's, e.g. partgam.ado and venndiag.ado, so it should not be a lack of Stata practice.
Let us solve issue 2 first - most likely simpler than #1.
2: I followed the worked example in:
https://blog.stata.com/2021/06/24/cu...lassic-table-1
and made a simple table with two variables sex (n - %) and age (mean - SD). As you can see here:
but notice number of decimals on percentages, e.g. 43.69874
The percentages in column 2 and 4 are not following the "nformat" of 4.2f
----------------------------------------------
Hypertension
No Yes
----------------------------------------------
Sex
Male 2611 43.69874 2304 52.65082
Female 3364 56.30126 2072 47.34918
Age (years) 42 (16.8) 55 (14.9)
----------------------------------------------
Can anyone suggest a solution ?
cheers
Jens Lauritsen, prof.,M.d.
University of Southern Denmark
code used is shown in full below:
* version 17 // just to see if version was the problem.
webuse nhanes2l
* (Second National Health and Nutrition Examination Survey)
cls
collect clear
table (var) (highbp), ///
statistic(fvfrequency sex ) ///
statistic(fvpercent sex) ///
statistic(mean age) ///
statistic(sd age) ///
nototal
* reorder to get only counts and percentages plus mean age/SD
collect recode result fvfrequency = column1 ///
fvpercent = column2 ///
mean = column1 ///
sd = column2 ///
collect layout (var) (highbp#result[column1 column2])
set dp period
* numeric format of cells:
collect style cell var[age]#result[column2], nformat(%4.1f)
collect style cell var[sex age hlthstat]#result[column1], nformat(%6.0f)
* add () to age and % to percentages of sex:
collect style cell var[age]#result[column2], sformat("(%s)")
/*
I gave up on using
collect style cell var[sex]#result[column2], sformat("(%s)")
*/
collect style cell var[sex]#result[column2], nformat(%4.0f)
collect preview
* final touch:
collect style cell border_block, border(right, pattern(nil)) // borders
collect label dim highbp "Hypertension", modify // variable label
collect style row stack, nobinder spacer // stack to avoid "sex="
collect label levels highbp 0 "No" 1 "Yes" // explanation
collect style header result, level(hide) // hide "column1 ....."
collect preview
1. I cannot use a binary variable for counting in the table. It seems to have to be a float, e.g. blood pressure or BMI
I tried to find a solution in "STATA CUSTOMIZABLE TABLES AND COLLECTED RESULTS REFERENCE MANUAL RELEASE 18", but with no luck. .
2. I cannot control number of decimals in percentages and add the "%" to that column estimates
Something is not working with collect and the formatting of cells. I am working on version 19.5 with all updates implemented.
I can do the same with a combination of collapse or simple counting and creating manual arrays with putdocx, but creating a targeted collection seems the optimal strategy from now onwards. I worked with Stata since about version 5 and have written many ado's, e.g. partgam.ado and venndiag.ado, so it should not be a lack of Stata practice.
Let us solve issue 2 first - most likely simpler than #1.
2: I followed the worked example in:
https://blog.stata.com/2021/06/24/cu...lassic-table-1
and made a simple table with two variables sex (n - %) and age (mean - SD). As you can see here:
but notice number of decimals on percentages, e.g. 43.69874
The percentages in column 2 and 4 are not following the "nformat" of 4.2f
----------------------------------------------
Hypertension
No Yes
----------------------------------------------
Sex
Male 2611 43.69874 2304 52.65082
Female 3364 56.30126 2072 47.34918
Age (years) 42 (16.8) 55 (14.9)
----------------------------------------------
Can anyone suggest a solution ?
cheers
Jens Lauritsen, prof.,M.d.
University of Southern Denmark
code used is shown in full below:
* version 17 // just to see if version was the problem.
webuse nhanes2l
* (Second National Health and Nutrition Examination Survey)
cls
collect clear
table (var) (highbp), ///
statistic(fvfrequency sex ) ///
statistic(fvpercent sex) ///
statistic(mean age) ///
statistic(sd age) ///
nototal
* reorder to get only counts and percentages plus mean age/SD
collect recode result fvfrequency = column1 ///
fvpercent = column2 ///
mean = column1 ///
sd = column2 ///
collect layout (var) (highbp#result[column1 column2])
set dp period
* numeric format of cells:
collect style cell var[age]#result[column2], nformat(%4.1f)
collect style cell var[sex age hlthstat]#result[column1], nformat(%6.0f)
* add () to age and % to percentages of sex:
collect style cell var[age]#result[column2], sformat("(%s)")
/*
I gave up on using
collect style cell var[sex]#result[column2], sformat("(%s)")
*/
collect style cell var[sex]#result[column2], nformat(%4.0f)
collect preview
* final touch:
collect style cell border_block, border(right, pattern(nil)) // borders
collect label dim highbp "Hypertension", modify // variable label
collect style row stack, nobinder spacer // stack to avoid "sex="
collect label levels highbp 0 "No" 1 "Yes" // explanation
collect style header result, level(hide) // hide "column1 ....."
collect preview

Comment