Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using the new table collect row percentages for multiple factor variables

    Hi all,

    I recently attended the webinar on customizable tables in Stata 17. One of the sample tables used in the webinar, as shown below in slighted modified, demonstrates the power of the new table/collect command.

    Code:
    -------------------------------------------------------
                               Hypertension                
                    No             Yes            Total    
    -------------------------------------------------------
    Sex                                                    
      Male    2,611   43.7%   2,304   52.7%   4,915   47.5%
      Female  3,364   56.3%   2,072   47.3%   5,436   52.5%
    Race                                                   
      White   5,317   89.0%   3,748   85.6%   9,065   87.6%
      Black     545    9.1%     541   12.4%   1,086   10.5%
      Other     113    1.9%      87    2.0%     200    1.9%
    -------------------------------------------------------


    Equally common is the need to show row percentages across the column variable, in this instance, the yes/no values of hypertension. Alternatively, in a slightly different arrangement, a table that combines the results from multiple tabstat results, showing total case count, count of cases with the yes value, and percent with yes, all properly formatted, would be very helpful as well. I searched without success any postings related this type of table using the new Stata 17 command table/collect. Shown below is the log result from tabstat using the same dataset in the webinar.

    Code:
    . webuse nhanes2, clear
    
    . tabstat highbp, by(sex) s(count sum mean)
    
    Summary for variables: highbp
    Group variable: sex (Sex)
    
       sex |         N       Sum      Mean
    -------+------------------------------
      Male |      4915      2304  .4687691
    Female |      5436      2072  .3811626
    -------+------------------------------
     Total |     10351      4376  .4227611
    --------------------------------------
    
    . tabstat highbp, by(race) s(count sum mean)
    
    Summary for variables: highbp
    Group variable: race (Race)
    
      race |         N       Sum      Mean
    -------+------------------------------
     White |      9065      3748  .4134584
     Black |      1086       541  .4981584
     Other |       200        87      .435
    -------+------------------------------
     Total |     10351      4376  .4227611
    --------------------------------------
    Any help will be greatly appreciated.

    Best,
    Ron

  • #2
    Hi Ron. You can use -table- to generate each of the tables you show as examples. Just make sure the second time you use the option -append-, so the results are added to the Table collection rather than replacing it. Then modify the layout to show both race and sex in the rows and the results in the columns. In the code below I also changed the label of count from the default "Number of non-missing values" to just "N"..

    Code:
    webuse nhanes2, clear
    table sex, statistic(count highbp) statistic(sum highbp) ///
        statistic(mean highbp) 
    table race, statistic(count highbp) statistic(sum highbp) ///
        statistic(mean highbp) append
    collect label levels result count "N", modify
    collect layout (sex race) (result)
    You could run the -table- commands -quietly-, but it probably helps to see what you get at each step. This is what I got in the end:

    Code:
    -------------------------------------
             |       N   Total       Mean
    ---------+---------------------------
    Sex      |                           
      Male   |   4,915   2,304   .4687691
      Female |   5,436   2,072   .3811626
      Total  |  10,351   4,376   .4227611
    Race     |                           
      White  |   9,065   3,748   .4134584
      Black  |   1,086     541   .4981584
      Other  |     200      87       .435
      Total  |  10,351   4,376   .4227611
    -------------------------------------
    Hope this helps.

    Comment


    • #3
      Thanks so much, German, for your help. Exactly what I needed.

      Best,
      Ron

      Comment


      • #4
        Hi all,

        I'm also trying to get row percentages using the new table command in Stata17 but can only find the factor variable percentage as a statistic.

        webuse nhanes2, clear
        table (var) (hlthstat), statistic(fvfrequency female) statistic(fvpercent female) nototals
        collect recode result fvfrequency = column1 fvpercent = column2
        collect layout (var) (hlthstat#result[column1 column2])



        -----------------------------------------------------------------------------------------------------------------------------------------------
        | Health status
        | Excellent Very good Good Fair Poor Blank but applicable
        | column1 column2 column1 column2 column1 column2 column1 column2 column1 column2 column1 column2
        --------------+--------------------------------------------------------------------------------------------------------------------------------
        Female=Male | 1252 52.01496 1213 46.8159 1340 45.60926 722 43.23353 382 52.40055 6 42.85714
        Female=Female | 1155 47.98504 1378 53.1841 1598 54.39074 948 56.76647 347 47.59945 8 57.14286
        -----------------------------------------------------------------------------------------------------------------------------------------------


        the numbers in column2 the table above are the factor variable percentages, but I'm hoping to calculate the row percentages (in addition to showing the frequencies in column1)

        i.e. instead of 1252/(1252+1155) = 52.01 in the table above, I want it to generate 1252 / (1252+1213+1340+722+382+6) = 0.25

        I presume this is possible but can't seem to work it out. If someone could help that would be much appreciated

        Kind regards,
        Ben

        Comment


        • #5
          I should probably also add that I'm hoping to be able to create a table for multiple row variables so:

          webuse nhanes2, clear
          table female hlthstat, statistic(frequency) statistic(percent, across(hlthstat))


          is not sufficient.

          I've also tried using append to add extra rows to the table:

          table female hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals
          table region hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals append
          collect layout (female region) (result)


          but get the error: Your layout specification does not uniquely match any items. One or more of the following dimensions might help uniquely match items: across,
          cmdset, hlthstat, statcmd.

          Comment


          • #6
            You have to write:
            Code:
            . collect layout (female#result region#result) (hlthstat)

            Comment


            • #7
              Thanks very much Mark!

              Comment


              • #8
                Is there a way I can get all the percent statistic to be on the same row as the frequency statistic? (see the below attachment)

                I've attempted various interaction terms but haven't been able to achieve this

                Click image for larger version

Name:	image.png
Views:	1
Size:	22.0 KB
ID:	1680231

                Comment


                • #9
                  Code:
                  collect layout (female region) (hlthstat#result)

                  Comment


                  • #10
                    Thanks Hemanshu!

                    I'm also trying to get a row at the top of the table showing the total number of patients in each column (see attached image below)

                    I tried the following code:

                    webuse nhanes2, clear
                    table (var) (hlthstat), statistic(count hlthstat) nototals
                    table female hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals append
                    table region hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals append
                    collect layout (var female region) (hlthstat#result)

                    but I still only get a table with female and region (without totals at the top)

                    Is there a way to get a row at the top of the table showing the total number of patients in each column?

                    Click image for larger version

Name:	image3.png
Views:	1
Size:	34.0 KB
ID:	1680415



                    Kind regards,
                    Ben



                    Attached Files

                    Comment


                    • #11
                      Try this?
                      Code:
                      webuse nhanes2, clear
                      table (var) (hlthstat), statistic(count hlthstat) nototals
                      table female hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals append
                      table region hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals append
                      collect recode result count = frequency
                      collect label levels var hlthstat "N", modify
                      collect layout (var female region) (hlthstat#result)
                      noi collect preview
                      which produces:

                      Code:
                      ------------------------------------------------------------------------------------------------------------------------------------------------
                               |                                                              Health status                                                           
                               |       Excellent             Very good                Good                  Fair                  Poor           Blank but applicable 
                               |  Frequency   Percent   Frequency   Percent   Frequency   Percent   Frequency   Percent   Frequency   Percent     Frequency    Percent
                      ---------+--------------------------------------------------------------------------------------------------------------------------------------
                      N        |      2,407                 2,591                 2,938                 1,670                   729                      14           
                      Female   |                                                                                                                                      
                        Male   |      1,252     25.47       1,213     24.68       1,340     27.26         722     14.69         382      7.77             6       0.12
                        Female |      1,155     21.26       1,378     25.36       1,598     29.41         948     17.45         347      6.39             8       0.15
                      Region   |                                                                                                                                      
                        NE     |        562     26.83         558     26.63         631     30.12         257     12.27          77      3.68            10       0.48
                        MW     |        730     26.33         721     26.00         735     26.51         419     15.11         167      6.02             1       0.04
                        S      |        546     19.14         651     22.82         807     28.29         532     18.65         317     11.11                         
                        W      |        569     21.65         661     25.15         765     29.11         462     17.58         168      6.39             3       0.11
                      ------------------------------------------------------------------------------------------------------------------------------------------------
                      Last edited by Hemanshu Kumar; 01 Sep 2022, 22:26.

                      Comment


                      • #12
                        Amazing thanks Hemanshu!

                        Comment


                        • #13
                          Originally posted by Hemanshu Kumar View Post
                          Try this?
                          Code:
                          webuse nhanes2, clear
                          table (var) (hlthstat), statistic(count hlthstat) nototals
                          table female hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals append
                          table region hlthstat, statistic(frequency) statistic(percent, across(hlthstat)) nototals append
                          collect recode result count = frequency
                          collect label levels var hlthstat "N", modify
                          collect layout (var female region) (hlthstat#result)
                          noi collect preview
                          which produces:

                          Code:
                          ------------------------------------------------------------------------------------------------------------------------------------------------
                          | Health status
                          | Excellent Very good Good Fair Poor Blank but applicable
                          | Frequency Percent Frequency Percent Frequency Percent Frequency Percent Frequency Percent Frequency Percent
                          ---------+--------------------------------------------------------------------------------------------------------------------------------------
                          N | 2,407 2,591 2,938 1,670 729 14
                          Female |
                          Male | 1,252 25.47 1,213 24.68 1,340 27.26 722 14.69 382 7.77 6 0.12
                          Female | 1,155 21.26 1,378 25.36 1,598 29.41 948 17.45 347 6.39 8 0.15
                          Region |
                          NE | 562 26.83 558 26.63 631 30.12 257 12.27 77 3.68 10 0.48
                          MW | 730 26.33 721 26.00 735 26.51 419 15.11 167 6.02 1 0.04
                          S | 546 19.14 651 22.82 807 28.29 532 18.65 317 11.11
                          W | 569 21.65 661 25.15 765 29.11 462 17.58 168 6.39 3 0.11
                          ------------------------------------------------------------------------------------------------------------------------------------------------
                          Dear Hemanshu, how to include summary statistics for quantitative variables (i.e., Mean and SD) and p-value from different statistical tests into the table?

                          Thank you.

                          Stata 17

                          Comment


                          • #14
                            Hi all in the forum,
                            i would like to produce a percentage table over 8 years, i found difficult. i used table commands it gives me a range of percentage from 0 to 2. i would like to produce a single percentage for variables over 8 years. My dataset over view as follows:
                            Year Firmaccessinformation Firminformation Managementcommitmenttoelimina Determineifmineralsaresource PracticeoftheOECDsduedilig
                            2014 2 2 0 1 1
                            2015 2 2 2 1 1
                            2016 2 0 2 1 2
                            2017 2 2 2 1 2
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 0 1 4
                            2015 2 2 2 1 3
                            2016 2 2 2 1 2
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 1 4
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 0 2 1
                            2015 2 2 2 1 0
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 0 0 2 1 4
                            2014 2 2 2 1 4
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 1 1 1
                            2015 2 2 2 1 2
                            2016 2 2 2 1 2
                            2017 2 2 2 1 2
                            2018 2 2 2 1 2
                            2019 2 2 2 1 2
                            2020 2 2 2 1 2
                            2021 2 2 2 1 2
                            2014 2 2 2 1 4
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 1 1
                            2015 2 2 2 1 1
                            2016 2 2 2 1 1
                            2017 2 2 2 1 1
                            2018 2 2 2 1 1
                            2019 2 2 2 1 1
                            2020 2 2 2 1 1
                            2021 2 2 2 1 1
                            2014 2 2 1 1 2
                            2015 2 2 1 2 2
                            2016 2 2 1 1 2
                            2017 2 2 1 2 2
                            2018 2 2 1 1 2
                            2019 2 2 1 1 2
                            2020 2 2 1 1 2
                            2021 2 2 1 1 2
                            2014 2 2 1 2 1
                            2015 2 2 1 2 1
                            2016 2 2 1 2 1
                            2017 2 2 1 2 1
                            2018 2 2 1 2 1
                            2019 2 2 1 2 1
                            2020 2 2 1 2 1
                            2021 2 2 1 2 1
                            2014 2 2 2 1 4
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 2 4
                            2019 2 2 2 2 4
                            2020 2 2 2 2 4
                            2021 2 2 2 2 4
                            2014 2 2 1 1 2
                            2015 2 2 1 1 2
                            2016 2 2 1 1 2
                            2017 2 2 1 1 2
                            2018 2 2 1 1 1
                            2019 2 2 1 1 2
                            2020 2 2 1 1 2
                            2021 2 2 1 1 2
                            2014 2 2 1 2 1
                            2015 2 2 1 2 1
                            2016 2 2 1 2 1
                            2017 2 2 1 2 1
                            2018 2 2 1 2 1
                            2019 2 2 1 2 1
                            2020 2 2 1 2 1
                            2021 2 2 1 2 1
                            2014 2 2 0 2 1
                            2015 2 2 0 2 1
                            2016 2 2 0 2 1
                            2017 2 2 0 2 1
                            2018 2 2 0 2 1
                            2019 2 2 0 2 1
                            2020 2 2 0 2 1
                            2021 2 2 0 2 1
                            2014 2 2 1 2 1
                            2015 2 2 1 2 1
                            2016 2 2 1 2 1
                            2017 2 2 1 2 1
                            2018 2 2 1 2 1
                            2019 2 2 1 2 1
                            2020 2 2 1 2 1
                            2021 2 2 1 2 1
                            2014 2 2 0 1 1
                            2015 2 2 0 1 1
                            2016 2 2 0 1 1
                            2017 2 2 0 1 1
                            2018 2 2 0 1 1
                            2019 2 2 0 1 1
                            2020 2 2 0 1 1
                            2021 2 2 0 1 1
                            2014 2 2 0 1 1
                            2015 2 2 2 1 1
                            2016 2 2 2 1 1
                            2017 2 2 2 1 1
                            2018 2 2 2 1 1
                            2019 2 2 2 1 1
                            2020 2 2 2 1 1
                            2021 2 2 2 1 1
                            2014 2 2 2 1 2
                            2015 2 2 2 1 2
                            2016 2 2 2 1 3
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 1 2
                            2015 2 2 2 1 2
                            2016 2 2 2 1 2
                            2017 2 2 2 1 2
                            2018 2 2 2 1 2
                            2019 2 2 2 1 2
                            2020 2 2 2 1 2
                            2021 2 2 2 1 2
                            2014 2 2 0 2 1
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 1 2
                            2015 2 2 2 1 2
                            2016 2 2 2 1 2
                            2017 2 2 2 1 2
                            2018 2 2 2 1 2
                            2019 2 2 2 1 2
                            2020 2 2 2 1 2
                            2021 2 2 2 1 2
                            2014 2 2 0 2 0
                            2015 2 2 0 2 0
                            2016 2 2 0 2 0
                            2017 2 2 0 2 0
                            2018 2 2 0 2 0
                            2019 2 2 0 2 0
                            2020 2 2 0 2 0
                            2021 2 2 0 2 0
                            2014 2 2 2 2 0
                            2015 2 2 2 2 0
                            2016 2 2 2 1 4
                            2017 2 2 2 2 3
                            2018 2 2 2 2 4
                            2019 2 2 2 0 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 3
                            2014 2 2 1 1 4
                            2015 2 2 1 1 4
                            2016 2 2 1 1 4
                            2017 2 2 1 1 4
                            2018 2 2 1 1 4
                            2019 2 2 1 1 4
                            2020 2 2 1 1 4
                            2021 2 2 1 1 4
                            2014 2 2 0 1 1
                            2015 2 2 0 1 1
                            2016 2 2 0 2 1
                            2017 2 2 0 2 1
                            2018 2 2 0 1 0
                            2019 2 2 0 1 4
                            2020 2 2 0 1 4
                            2021 2 2 0 1 4
                            2014 2 2 1 1 0
                            2015 2 2 1 1 0
                            2016 2 2 1 1 0
                            2017 2 2 1 1 0
                            2018 2 2 1 1 0
                            2019 2 2 1 1 0
                            2020 2 2 1 1 0
                            2021 2 2 1 1 0
                            2014 2 2 2 2 4
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 1 1 1
                            2015 2 2 1 1 1
                            2016 2 2 1 1 1
                            2017 2 2 1 1 1
                            2018 2 2 1 1 1
                            2019 2 2 1 2 0
                            2020 2 2 1 2 0
                            2021 2 2 1 2 0
                            2014 2 2 2 2 0
                            2015 2 2 2 2 0
                            2016 2 2 2 2 0
                            2017 2 2 2 2 0
                            2018 2 2 2 2 0
                            2019 2 2 2 2 0
                            2020 2 2 2 2 0
                            2021 2 2 2 2 0
                            2014 2 2 2 1 1
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 1 1 1
                            2015 2 2 1 1 1
                            2016 2 2 1 1 1
                            2017 2 2 1 1 1
                            2018 2 2 1 1 1
                            2019 2 2 1 1 1
                            2020 2 2 1 1 1
                            2021 2 2 1 1 1
                            2014 2 2 1 1 1
                            2015 2 2 1 1 1
                            2016 2 2 1 1 1
                            2017 2 2 1 1 1
                            2018 2 2 1 1 1
                            2019 2 2 1 1 1
                            2020 2 2 1 1 1
                            2021 2 2 1 1 1
                            2014 2 2 2 1 3
                            2015 2 2 2 1 3
                            2016 2 2 2 2 3
                            2017 2 2 2 2 3
                            2018 2 2 2 2 3
                            2019 2 2 2 2 4
                            2020 2 2 2 2 4
                            2021 2 2 2 2 4
                            2014 2 2 2 1 0
                            2015 2 2 2 1 0
                            2016 2 2 2 1 1
                            2017 2 2 2 1 3
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 1 2
                            2015 2 2 2 1 2
                            2016 2 2 2 1 2
                            2017 2 2 2 1 2
                            2018 2 2 2 0 2
                            2019 2 2 2 0 2
                            2020 2 2 2 0 2
                            2021 2 2 2 0 2
                            2014 2 2 0 0 1
                            2015 2 2 0 0 1
                            2016 2 2 1 1 4
                            2017 2 2 1 0 4
                            2018 2 2 1 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 1 1 4
                            2014 2 2 2 1 4
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 2 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 1 4
                            2015 2 2 2 1 1
                            2016 2 2 2 1 1
                            2017 2 2 2 1 1
                            2018 2 2 2 1 1
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 1 4
                            2015 2 2 2 1 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 2 0 4
                            2015 2 2 2 0 4
                            2016 2 2 2 1 4
                            2017 2 2 2 1 4
                            2018 2 2 2 1 4
                            2019 2 2 2 1 4
                            2020 2 2 2 1 4
                            2021 2 2 2 1 4
                            2014 2 2 1 2 1
                            2015 2 2 1 2 1
                            2016 2 2 1 2 1
                            2017 2 2 1 2 1
                            2018 2 2 1 2 1
                            2019 2 2 1 2 1
                            2020 2 2 1 0 1
                            2021 2 2 1 2 1
                            2014 2 2 1 1 3
                            2015 2 2 1 1 3
                            2016 2 2 1 1 3
                            2017 2 2 1 1 3
                            2018 2 2 1 1 3
                            2019 2 2 1 1 3
                            2020 2 2 1 1 3
                            2021 2 2 1 1 1
                            2014 2 2 1 0 0
                            2015 2 2 1 0 0
                            2016 2 2 1 0 0
                            2017 2 2 1 0 0
                            2018 2 2 1 1 0
                            2019 2 2 1 1 0
                            2020 2 2 1 1 0
                            2021 2 2 1 1 0
                            2014 2 2 1 1 1
                            2015 2 2 1 1 1
                            2016 2 2 1 1 1
                            2017 2 2 1 1 1
                            2018 2 2 1 1 1
                            2019 2 2 1 1 1
                            2020 2 2 1 1 1
                            2021 2 2 1 1 1
                            2014 2 2 1 1 2
                            2015 2 2 1 1 2
                            2016 2 2 1 1 2
                            2017 2 2 1 1 2
                            2018 2 2 1 1 2
                            2019 2 2 1 1 2
                            2020 2 2 1 1 2
                            2021 2 2 1 1 2

                            I would like to produce similar table attached in the discussion.
                            I assume this is possible but can't seem to work for me. If someone could help that would be much appreciated

                            Best regards,
                            Babikir
                            Attached Files

                            Comment


                            • #15
                              x
                              Last edited by Babikir Bechir; 11 Jul 2023, 05:34.

                              Comment

                              Working...
                              X