Announcement

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

  • Multiway Table, hide certain levels of variable

    I am using this manual, on p10:
    rtablemultiway.pdf (stata.com)

    I want to create this table (see img), but only showing the percentages with "yes" (the right columns for each variable). How do I do this?
    Click image for larger version

Name:	Screenshot 2024-08-26 141330.png
Views:	1
Size:	113.3 KB
ID:	1762357

  • #2
    Code:
    help collect levelsof
    Code:
    use https://www.stata-press.com/data/r18/nhanes2l, clear
    label define yesno 0 "No" 1 "Yes"
    label values highbp diabetes heartatk yesno
    label variable diabetes "Diabetes"
    collect clear
    quietly: table (sex agegrp) (diabetes), nototals statistic(percent, across(diabetes)) name(table1)
    quietly: table (sex agegrp) (highbp), nototals statistic(percent, across(highbp)) name(table1) append
    quietly: table (sex agegrp) (heartatk), nototals statistic(percent, across(heartatk)) name(table1) append
    collect layout (sex#agegrp) (diabetes highbp heartatk)
    collect layout (sex#agegrp) (diabetes[1] highbp[1] heartatk[1])
    Res.:

    Code:
    . collect layout (sex#agegrp) (diabetes highbp heartatk)
    
    Collection: table1
          Rows: sex#agegrp
       Columns: diabetes highbp heartatk
       Table 1: 17 x 6
    
    -----------------------------------------------------------------------------------
                  |        Diabetes         High blood pressure     Prior heart attack 
                  |        No        Yes          No         Yes          No        Yes
    --------------+--------------------------------------------------------------------
    Sex           |                                                                    
      Male        |                                                                    
        Age group |                                                                    
          20–29   |  99.64158   .3584229    73.92473    26.07527         100           
          30–39   |  99.61039   .3896104    62.33766    37.66234    99.74026   .2597403
          40–49   |  97.37705   2.622951    55.08197    44.91803    98.03279   1.967213
          50–59   |  94.68439   5.315615     42.3588     57.6412     92.3588   7.641196
          60–69   |  91.96494   8.035062    41.49014    58.50986    86.55953   13.44047
          70+     |  88.39286   11.60714     32.8125     67.1875    83.48214   16.51786
      Female      |                                                                    
        Age group |                                                                    
          20–29   |  99.08638   .9136213     91.6113    8.388704    99.91694   .0830565
          30–39   |  97.88484   2.115159     80.6338     19.3662    99.76498   .2350176
          40–49   |  96.06657   3.933434    65.55891    34.44109    98.78971   1.210287
          50–59   |  94.19448   5.805515    48.62119    51.37881    96.66183   3.338171
          60–69   |  91.41516   8.584842    41.91818    58.08182     94.5674   5.432596
          70+     |  89.03346   10.96654    33.45725    66.54275    92.00743   7.992565
    -----------------------------------------------------------------------------------
    
    . 
    . collect layout (sex#agegrp) (diabetes[1] highbp[1] heartatk[1])
    
    Collection: table1
          Rows: sex#agegrp
       Columns: diabetes[1] highbp[1] heartatk[1]
       Table 1: 17 x 3
    
    --------------------------------------------------------------------
                  |  Diabetes   High blood pressure   Prior heart attack
                  |       Yes                   Yes                  Yes
    --------------+-----------------------------------------------------
    Sex           |                                                     
      Male        |                                                     
        Age group |                                                     
          20–29   |  .3584229              26.07527                     
          30–39   |  .3896104              37.66234             .2597403
          40–49   |  2.622951              44.91803             1.967213
          50–59   |  5.315615               57.6412             7.641196
          60–69   |  8.035062              58.50986             13.44047
          70+     |  11.60714               67.1875             16.51786
      Female      |                                                     
        Age group |                                                     
          20–29   |  .9136213              8.388704             .0830565
          30–39   |  2.115159               19.3662             .2350176
          40–49   |  3.933434              34.44109             1.210287
          50–59   |  5.805515              51.37881             3.338171
          60–69   |  8.584842              58.08182             5.432596
          70+     |  10.96654              66.54275             7.992565
    --------------------------------------------------------------------
    
    .

    Comment


    • #3
      Thanks. The original tables basically include 2 decimals, but -collect layout- loses this format.

      -collect layout (male#agegrp5) (d_diab[1] d_hibp[1]), nformat(%9.2f percent)- does not work

      How do I keep only 2 decimals in this case?

      Comment


      • #4
        The dimension is sex and "Male" is a level of this dimension. You can apply the formatting as below:

        Code:
        use https://www.stata-press.com/data/r18/nhanes2l, clear
        label define yesno 0 "No" 1 "Yes"
        label values highbp diabetes heartatk yesno
        label variable diabetes "Diabetes"
        collect clear
        quietly: table (sex agegrp) (diabetes), nototals statistic(percent, across(diabetes)) name(table1)
        quietly: table (sex agegrp) (highbp), nototals statistic(percent, across(highbp)) name(table1) append
        quietly: table (sex agegrp) (heartatk), nototals statistic(percent, across(heartatk)) name(table1) append
        collect layout (sex#agegrp) (diabetes highbp heartatk)
        collect style cell diabetes highbp, nformat(%3.2f)
        collect layout (sex[1]#agegrp) (diabetes[1] highbp[1])
        Res.:

        Code:
        . collect style cell diabetes highbp, nformat(%3.2f)
        
        . 
        . collect layout (sex[1]#agegrp) (diabetes[1] highbp[1])
        
        Collection: table1
              Rows: sex[1]#agegrp
           Columns: diabetes[1] highbp[1]
           Table 1: 9 x 2
        
        -----------------------------------------------
                      |  Diabetes   High blood pressure
                      |       Yes                   Yes
        --------------+--------------------------------
        Sex           |                                
          Male        |                                
            Age group |                                
              20–29   |      0.36                 26.08
              30–39   |      0.39                 37.66
              40–49   |      2.62                 44.92
              50–59   |      5.32                 57.64
              60–69   |      8.04                 58.51
              70+     |     11.61                 67.19
        -----------------------------------------------
        
        .

        Comment


        • #5
          Thank you so much again.
          Sorry to ask one more question. The manual on -collect- is very extensive and it can be hard to find some things.

          How do I add the number of nonmissing observations to this? The table I create in this code includes this:
          -quietly: table (sex agegrp) (diabetes), nototals statistic(percent, across(diabetes)) statistic(count diabetes) name(table1)-

          Comment


          • #6
            You should focus on looking at dimensions of the table and levels of these dimensions to understand how to modify and construct tables.

            Code:
            help collect dims
            Code:
            use https://www.stata-press.com/data/r18/nhanes2l, clear
            label define yesno 0 "No" 1 "Yes"
            label values highbp diabetes heartatk yesno
            label variable diabetes "Diabetes"
            collect clear
            quietly: table (sex agegrp) (diabetes), nototals statistic(percent, across(diabetes)) statistic(count diabetes) name(table1)
            quietly: table (sex agegrp) (highbp), nototals statistic(percent, across(highbp)) statistic(count highbp) name(table1) append
            quietly: table (sex agegrp) (heartatk), nototals statistic(percent, across(heartatk)) statistic(count heartatk) name(table1) append
            
            *LOOK AT DIMENSIONS OF TABLE
            collect dims
            
            *LOOK AT LEVELS OF DIMENSION RESULT
            collect levelsof result
            
            *FORMAT CELLS
            collect style cell result[percent], nformat(%3.2f)
            collect style cell result[count], nformat(%9.0f)
            
            collect layout (sex#agegrp) (result#(diabetes[1] highbp[1] heartatk[1]))
            You can change the column labels.

            Res.:

            Code:
            . collect layout (sex#agegrp) (result#(diabetes[1] highbp[1] heartatk[1]))
            
            Collection: table1
                  Rows: sex#agegrp
               Columns: result#(diabetes[1] highbp[1] heartatk[1])
               Table 1: 17 x 6
            
            --------------------------------------------------------------------------------------------------------------------------------------------------------------
                          |   Percent   Number of nonmissing values               Percent   Number of nonmissing values              Percent   Number of nonmissing values
                          |  Diabetes             Diabetes            High blood pressure       High blood pressure       Prior heart attack        Prior heart attack    
                          |       Yes                           Yes                   Yes                           Yes                  Yes                           Yes
            --------------+-----------------------------------------------------------------------------------------------------------------------------------------------
            Sex           |                                                                                                                                              
              Male        |                                                                                                                                              
                Age group |                                                                                                                                              
                  20–29   |      0.36                             4                 26.08                           291                                                  
                  30–39   |      0.39                             3                 37.66                           290                 0.26                             2
                  40–49   |      2.62                            16                 44.92                           274                 1.97                            12
                  50–59   |      5.32                            32                 57.64                           347                 7.64                            46
                  60–69   |      8.04                           110                 58.51                           801                13.44                           184
                  70+     |     11.61                            52                 67.19                           301                16.52                            74
              Female      |                                                                                                                                              
                Age group |                                                                                                                                              
                  20–29   |      0.91                            11                  8.39                           101                 0.08                             1
                  30–39   |      2.12                            18                 19.37                           165                 0.24                             2
                  40–49   |      3.93                            26                 34.44                           228                 1.21                             8
                  50–59   |      5.81                            40                 51.38                           354                 3.34                            23
                  60–69   |      8.58                           128                 58.08                           866                 5.43                            81
                  70+     |     10.97                            59                 66.54                           358                 7.99                            43
            --------------------------------------------------------------------------------------------------------------------------------------------------------------
            
            .
            Last edited by Andrew Musau; 26 Aug 2024, 13:31.

            Comment


            • #7

              How do I change the label "Number of nonmissing values" to "N"?
              Last edited by Castor Comploj; 20 Oct 2024, 08:54.

              Comment


              • #8
                Also, in addition to #7, the solution above is not complete. The number of nonmissing observations that I need in this case is the total yes/no, not only those with diabetes==yes.
                See this, the 4 is the one that came out from your answer #6:
                HTML Code:
                . bys sex: tab agegrp diab
                
                -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                -> sex = Male
                
                           |       Diabetes
                 Age group |        No        Yes |     Total
                -----------+----------------------+----------
                     20–29 |     1,112          4 |     1,116 
                     30–39 |       767          3 |       770 
                     40–49 |       594         16 |       610 
                     50–59 |       570         32 |       602 
                     60–69 |     1,259        110 |     1,369 
                       70+ |       396         52 |       448 
                -----------+----------------------+----------
                     Total |     4,698        217 |     4,915 

                Comment

                Working...
                X