Announcement

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

  • dtable: How to combine collections that add rows instead of colums

    Hi,

    I'm looking for a way of using -dtable- to generate a table of summary statistics using different weights variables. I'm using the example provided in the Stata Manual, pp. 570-572 because I'm interested in describing variables across groups.

    Using the following example, is there a way of combining "part1" and "part2" into a single table, where "part2" is added at the bottom of the combined table?

    Code:
    use https://www.stata-press.com/data/r18/nhanes2l, clear
    
    svyset
    
    Sampling weights: finalwgt
                 VCE: linearized
         Single unit: missing
            Strata 1: strata
     Sampling unit 1: psu
               FPC 1: <zero>
    
    des finalwgt leadwt
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ----------------------------------------------------------------------------------
    finalwgt        long    %9.0g                 Sampling weight (except lead)
    leadwt          long    %9.0g                 Sampling weight for lead
    Code:
    * Weighted summary statistics (for all variables but lead exposure)
    dtable bpsystol age weight i.race i.hlthstat, by(rural) svy    name(part1) ///
        nformat(%16.1fc mean sd) column(summary(M(SD) / n(%)))    ///
    
    
    ----------------------------------------------------------------------------------
                                                       Rural                          
                                   Urban              Rural               Total       
    ----------------------------------------------------------------------------------
    N                       79,965,794 (68.3%) 37,191,719 (31.7%) 117,157,513 (100.0%)
    Systolic blood pressure       126.6 (21.4)       127.7 (21.3)         126.9 (21.4)
    Age (years)                    41.8 (15.7)        43.2 (15.1)          42.3 (15.5)
    Weight (kg)                    71.3 (15.4)        73.1 (15.5)          71.9 (15.4)
    Race                                                                              
      White                 67,579,394 (84.5%) 35,420,155 (95.2%)  102,999,549 (87.9%)
      Black                  9,936,159 (12.4%)   1,253,077 (3.4%)    11,189,236 (9.6%)
      Other                   2,450,241 (3.1%)     518,487 (1.4%)     2,968,728 (2.5%)
    Health status                                                                     
      Excellent             22,781,784 (28.5%)  9,405,551 (25.3%)   32,187,335 (27.5%)
      Very good             22,867,496 (28.6%)  9,308,814 (25.1%)   32,176,310 (27.5%)
      Good                  22,089,942 (27.7%) 10,625,453 (28.6%)   32,715,395 (28.0%)
      Fair                   8,892,926 (11.1%)  5,487,335 (14.8%)   14,380,261 (12.3%)
      Poor                    3,229,798 (4.0%)   2,308,158 (6.2%)     5,537,956 (4.7%)
    ----------------------------------------------------------------------------------
    Code:
    * Weighted summary statistics (for lead exposure)
    dtable lead [fw=leadwt], by(rural) name(part2) ///
        nformat(%16.1fc mean sd) column(summary(M(SD) / n(%)))
    
    ------------------------------------------------------------------------
                                             Rural                          
                         Urban              Rural               Total       
    ------------------------------------------------------------------------
    N             79,625,832 (68.2%) 37,173,234 (31.8%) 116,799,066 (100.0%)
    Lead (mcg/dL)         14.8 (6.3)         13.5 (6.1)           14.4 (6.2)
    ------------------------------------------------------------------------
    I found this Statalist thread instructive... but, there, people combine groups by adding new columns, and here I would like to generate a combined table preserving the grouping by(rural).

    Thank you!

  • #2
    Using your example, here is the code I used to stack your dtables.
    Code:
    * create a new collection by combining the parts
    collect combine whole = part1 part2
    
    * this layout is taken from part1, and puts the lead results from part2
    * with the other results from part1, but the sample results (N) are
    * missing since they are present in both parts
    collect layout
    
    * -collect combine- creates a new dimension named -collection-;
    * list the levels to see it contains part1 and part2
    collect levelsof collection
    
    * put collection in the layout to stack the parts
    collect layout (collection#var) (rural#result)
    
    * add fancy labels for the parts
    collect label levels collection part1 "Part 1" part2 "Part 2"
    collect preview
    Here is the resulting table
    Code:
    . collect preview
    
    ------------------------------------------------------------------------------------
                                                         Rural                          
                                     Urban              Rural               Total       
    ------------------------------------------------------------------------------------
    Part 1                                                                              
      N                       79,965,794 (68.3%) 37,191,719 (31.7%) 117,157,513 (100.0%)
      Systolic blood pressure       126.6 (21.4)       127.7 (21.3)         126.9 (21.4)
      Age (years)                    41.8 (15.7)        43.2 (15.1)          42.3 (15.5)
      Weight (kg)                    71.3 (15.4)        73.1 (15.5)          71.9 (15.4)
      Race                                                                              
        White                 67,579,394 (84.5%) 35,420,155 (95.2%)  102,999,549 (87.9%)
        Black                  9,936,159 (12.4%)   1,253,077 (3.4%)    11,189,236 (9.6%)
        Other                   2,450,241 (3.1%)     518,487 (1.4%)     2,968,728 (2.5%)
      Health status                                                                     
        Excellent             22,781,784 (28.5%)  9,405,551 (25.3%)   32,187,335 (27.5%)
        Very good             22,867,496 (28.6%)  9,308,814 (25.1%)   32,176,310 (27.5%)
        Good                  22,089,942 (27.7%) 10,625,453 (28.6%)   32,715,395 (28.0%)
        Fair                   8,892,926 (11.1%)  5,487,335 (14.8%)   14,380,261 (12.3%)
        Poor                    3,229,798 (4.0%)   2,308,158 (6.2%)     5,537,956 (4.7%)
    Part 2                                                                              
      N                       79,625,832 (68.2%) 37,173,234 (31.8%) 116,799,066 (100.0%)
      Lead (mcg/dL)                   14.8 (6.3)         13.5 (6.1)           14.4 (6.2)
    ------------------------------------------------------------------------------------

    Comment


    • #3
      Hi Jeff Pitblado (StataCorp) , thank you so much for the answer! I'll try it asap.

      Comment


      • #4
        Originally posted by Jeff Pitblado (StataCorp) View Post
        Using your example, here is the code I used to stack your dtables.
        Code:
        * create a new collection by combining the parts
        collect combine whole = part1 part2
        
        * this layout is taken from part1, and puts the lead results from part2
        * with the other results from part1, but the sample results (N) are
        * missing since they are present in both parts
        collect layout
        
        * -collect combine- creates a new dimension named -collection-;
        * list the levels to see it contains part1 and part2
        collect levelsof collection
        
        * put collection in the layout to stack the parts
        collect layout (collection#var) (rural#result)
        
        * add fancy labels for the parts
        collect label levels collection part1 "Part 1" part2 "Part 2"
        collect preview
        Here is the resulting table
        Code:
        . collect preview
        
        ------------------------------------------------------------------------------------
        Rural
        Urban Rural Total
        ------------------------------------------------------------------------------------
        Part 1
        N 79,965,794 (68.3%) 37,191,719 (31.7%) 117,157,513 (100.0%)
        Systolic blood pressure 126.6 (21.4) 127.7 (21.3) 126.9 (21.4)
        Age (years) 41.8 (15.7) 43.2 (15.1) 42.3 (15.5)
        Weight (kg) 71.3 (15.4) 73.1 (15.5) 71.9 (15.4)
        Race
        White 67,579,394 (84.5%) 35,420,155 (95.2%) 102,999,549 (87.9%)
        Black 9,936,159 (12.4%) 1,253,077 (3.4%) 11,189,236 (9.6%)
        Other 2,450,241 (3.1%) 518,487 (1.4%) 2,968,728 (2.5%)
        Health status
        Excellent 22,781,784 (28.5%) 9,405,551 (25.3%) 32,187,335 (27.5%)
        Very good 22,867,496 (28.6%) 9,308,814 (25.1%) 32,176,310 (27.5%)
        Good 22,089,942 (27.7%) 10,625,453 (28.6%) 32,715,395 (28.0%)
        Fair 8,892,926 (11.1%) 5,487,335 (14.8%) 14,380,261 (12.3%)
        Poor 3,229,798 (4.0%) 2,308,158 (6.2%) 5,537,956 (4.7%)
        Part 2
        N 79,625,832 (68.2%) 37,173,234 (31.8%) 116,799,066 (100.0%)
        Lead (mcg/dL) 14.8 (6.3) 13.5 (6.1) 14.4 (6.2)
        ------------------------------------------------------------------------------------
        Hi Jeff, is it possible to expand your example to 4 or 6 dtables? I tried but without success

        Comment

        Working...
        X