Announcement

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

  • -collect-: how to format tables produced by -svy tab-?

    Please consider the following example which aims to summarize the essence of a problem a friend asked me to help with. I couldn't! Hence this post.
    Code:
    sysuse nhanes2, clear
    svyset
    
    foreach var of varlist agegrp hlthstat {
        svy: tab race `var' , row ci percent pearson
        svy: tab sex `var' , row ci  percent pearson
    }
    The ultimate goal is to export nicely-formatted tables to Excel or Word using -collect-, ideally with the 2 agegrp tables in one docx (or one Excel sheet) and the 2 hlthstat tables in another docx (or another sheet of same Excel doc).
    I can't see how to do this. My friend can get some of the way using -tabout- (Ian Watson, SSC), as follows:
    Code:
    capture ssc install tabout, replace
               * although -tabout- writes to text files only, it's possible to later open the text file with Excel
               * and get reasonably formatted tables  (say "yes" when Excel asks whether you want to open the file)
               * If the output file is being replaced, ensure that it's not open when  redoing your code
    foreach var of varlist agegrp hlthstat {
        forval x = 0/1 {
            tabout race sex `var' using `var'`x'.xls, ///
                c(row ci) f(1 1) clab(Row_% 95%_CI) svy ///
                stats(chi2) npos(lab) per replace
        }
    }
    I've tried to use -collect- and not got far. E.g., for one tabulation
    Code:
    collect clear
    svy: tab race hlthstat, row percent ci 
    collect get _r_b _r_ci
    This provides the basic raw material for one of the 4 tables.
    I looked at
    Code:
    collect dims
    which was not obviously helpful (how does one "see" what is put into each dimension?)
    I also experimented:
    Code:
    * coefficient [lower_bound  upper_bound]
    *     results for row 1, then row 2, etc
    collect layout (colname) (result)
    * is same as: 
    collect layout (colname) (result[_r_b _r_ci])
    
    *  coefficient 
    *  [lower_bound  upper_bound]
    collect layout (colname##result)
    Either set of output seems to be the place to start playing with -collect style- and other things to get it looking more like the -tabout- examples. But I made no progress. I can't find any -svytab- examples in the [T] Tables manual, nor relevant Statalist posts for this command. Nor was general web searching of useful help.

    Your advice and suggestions welcome -- thanks






  • #2
    Stephen,

    collect is an option under svy tab; see help svy: tabulate twoway and search on "collect"

    this may not make it any easier as I find myself a bit confused by what you write but, with luck, it will be relevant and helpful

    Rich

    Comment


    • #3
      Thanks, Rich. As far as I understand it, the option you refer to is simply the way to get the basic material, i.e., is the same as what I've already done at the first -collect- step in my experimenting. Apologies for any confusion. What my friend is after is the "Tips and Tricks" to reformat and export the -svy tab- tables so that they look like the -tabout- ones (at the mnimum]. I've used the nhanes2 dataset to illustrate the issues as anyone can access them. I didn't want to post attachments.

      Comment


      • #4
        In recent versions of Stata (at least version 19), -collect()- is an option of tabulate. So you can try something along the following lines:

        Code:
        webuse nhanes2, clear
        collect clear
        svyset
        
        foreach var of varlist hlthstat {
            svy: tab race `var' , row ci percent pearson collect(mytab, append) 
            svy: tab sex `var' , row ci percent pearson collect(mytab, append)
        }
        collect dims
        collect layout ((race sex)#result result) (hlthstat)
        Res.:

        Code:
        . collect layout ((race sex)#result result) (hlthstat)
        
        Collection: mytab
              Rows: (race sex)#result result
           Columns: hlthstat
           Table 1: 23 x 7
        
        -----------------------------------------------------------------------------------------------------------------------------
                                                                         Health status                                               
                             Excellent       Very good            Good            Fair           Poor   Blank but applicable    Total
        -----------------------------------------------------------------------------------------------------------------------------
        Race                                                                                                                         
          White                                                                                                                      
            Row percent          28.84           28.46           26.88           11.56           4.14                   0.13   100.00
            95% CI       [27.42 30.30]   [27.44 29.49]   [25.75 28.03]   [10.61 12.58]    [3.36 5.09]            [0.06 0.26]         
          Black                                                                                                                      
            Row percent          15.99           19.43           34.90           19.66          10.02                   0.00   100.00
            95% CI       [12.79 19.82]   [16.60 22.61]   [32.06 37.86]   [16.72 22.96]   [8.16 12.25]                                
          Other                                                                                                                      
            Row percent          23.62           23.56           38.22            9.41           5.18                   0.00   100.00
            95% CI       [15.57 34.15]   [16.48 32.51]   [30.04 47.13]    [6.00 14.44]    [3.05 8.68]                                
          Total                                                                                                                      
            Row percent          27.48           27.47           27.93           12.28           4.73                   0.11   100.00
            95% CI       [26.09 28.91]   [26.38 28.58]   [26.92 28.96]   [11.41 13.19]    [4.02 5.55]            [0.06 0.23]         
        Sex                                                                                                                          
          Male                                                                                                                       
            Row percent          29.94           27.32           27.01           10.66           5.01                   0.07   100.00
            95% CI       [28.32 31.60]   [25.58 29.12]   [25.50 28.56]    [9.68 11.72]    [4.16 6.03]            [0.02 0.23]         
          Female                                                                                                                     
            Row percent          25.22           27.61           28.78           13.77           4.46                   0.16   100.00
            95% CI       [23.24 27.30]   [26.24 29.02]   [27.25 30.36]   [12.59 15.04]    [3.70 5.38]            [0.07 0.37]         
          Total                                                                                                                      
            Row percent          27.48           27.47           27.93           12.28           4.73                   0.11   100.00
            95% CI       [26.09 28.91]   [26.38 28.58]   [26.92 28.96]   [11.41 13.19]    [4.02 5.55]            [0.06 0.23]         
        -----------------------------------------------------------------------------------------------------------------------------

        Comment


        • #5
          Here's the output from the commands cited above, followed a snippet from the -tabout- output.

          Code:
          . sysuse nhanes2, clear
          
          . svyset
          
          Sampling weights: finalwgt
                       VCE: linearized
               Single unit: missing
                  Strata 1: strata
           Sampling unit 1: psu
                     FPC 1: <zero>
          
          . 
          . foreach var of varlist agegrp hlthstat {
            2.         svy: tab race `var' , row ci percent pearson
            3.     svy: tab sex `var' , row ci  percent pearson
            4. }
          (running tabulate on estimation sample)
          
          Number of strata = 31                            Number of obs   =      10,351
          Number of PSUs   = 62                            Population size = 117,157,513
                                                           Design df       =          31
          
          -------------------------------------------------------------------------------
          1=white |
          ,       |
          2=black |
          ,       |                            Age groups 1-6                            
          3=other |      age20-29      age30-39      age40-49      age50-59      age60-69
          --------+----------------------------------------------------------------------
            White |         27.42         20.32         16.91         16.83         13.75
                  | [25.96,28.93]     [19,21.7] [15.98,17.89] [15.66,18.07] [12.93,14.61]
                  | 
            Black |         32.94         21.45         15.83         15.87         10.16
                  | [29.07,37.05] [18.92,24.21] [13.21,18.85] [13.57,18.49] [8.687,11.85]
                  | 
            Other |         31.24         20.57          18.1         15.99         11.55
                  |  [22.4,41.69]  [15.6,26.63] [14.38,22.53] [11.54,21.74]  [5.74,21.87]
                  | 
            Total |         28.05         20.43         16.84         16.72         13.35
                  |  [26.6,29.53] [19.28,21.63]  [16.01,17.7] [15.71,17.78] [12.58,14.16]
          -------------------------------------------------------------------------------
          
          -------------------------------------
          1=white |
          ,       |
          2=black |
          ,       |       Age groups 1-6       
          3=other |       age 70+         Total
          --------+----------------------------
            White |         4.777           100
                  | [4.154,5.489]              
                  | 
            Black |         3.751           100
                  | [2.809,4.993]              
                  | 
            Other |         2.544           100
                  |  [1.04,6.092]              
                  | 
            Total |         4.623           100
                  | [4.053,5.269]              
          -------------------------------------
          Key: Row percentage
               [95% confidence interval for row percentage]
          
            Pearson:
              Uncorrected   chi2(10)        =   26.4662
              Design-based  F(3.89, 120.70) =    1.7503     P = 0.1451
          (running tabulate on estimation sample)
          
          Number of strata = 31                            Number of obs   =      10,351
          Number of PSUs   = 62                            Population size = 117,157,513
                                                           Design df       =          31
          
          -------------------------------------------------------------------------------
          1=male, |
          2=femal |                            Age groups 1-6                            
          e       |      age20-29      age30-39      age40-49      age50-59      age60-69
          --------+----------------------------------------------------------------------
             Male |         28.35         20.97         16.99         16.42         13.09
                  | [26.45,30.33] [19.27,22.78] [15.81,18.23]  [15.03,17.9] [12.09,14.16]
                  | 
           Female |         27.77         19.93          16.7         16.99         13.59
                  | [26.06,29.54] [18.74,21.18] [15.68,17.77] [15.65,18.42] [12.77,14.46]
                  | 
            Total |         28.05         20.43         16.84         16.72         13.35
                  |  [26.6,29.53] [19.28,21.63]  [16.01,17.7] [15.71,17.78] [12.58,14.16]
          -------------------------------------------------------------------------------
          
          -------------------------------------
          1=male, |
          2=femal |       Age groups 1-6       
          e       |       age 70+         Total
          --------+----------------------------
             Male |         4.194           100
                  |  [3.63,4.842]              
                  | 
           Female |         5.018           100
                  |  [4.298,5.85]              
                  | 
            Total |         4.623           100
                  | [4.053,5.269]              
          -------------------------------------
          Key: Row percentage
               [95% confidence interval for row percentage]
          
            Pearson:
              Uncorrected   chi2(5)         =    6.5876
              Design-based  F(3.89, 120.53) =    1.2212     P = 0.3055
          (running tabulate on estimation sample)
          
          Number of strata = 31                            Number of obs   =      10,349
          Number of PSUs   = 62                            Population size = 117,131,111
                                                           Design df       =          31
          
          -------------------------------------------------------------------------------
          1=white |
          ,       |
          2=black |
          ,       |                        1=excellent,..., 5=poor                       
          3=other |             1             2             3             4             5
          --------+----------------------------------------------------------------------
            White |         28.84         28.46         26.88         11.56          4.14
                  |  [27.42,30.3] [27.44,29.49] [25.75,28.03] [10.61,12.58] [3.359,5.093]
                  | 
            Black |         15.99         19.43          34.9         19.66         10.02
                  | [12.79,19.82]  [16.6,22.61] [32.06,37.86] [16.72,22.96] [8.157,12.25]
                  | 
            Other |         23.62         23.56         38.22         9.407         5.185
                  | [15.57,34.15] [16.48,32.51] [30.04,47.13] [6.003,14.44] [3.052,8.675]
                  | 
            Total |         27.48         27.47         27.93         12.28         4.728
                  | [26.09,28.91] [26.38,28.58] [26.92,28.96] [11.41,13.19] [4.024,5.549]
          -------------------------------------------------------------------------------
          
          -------------------------------------
          1=white |
          ,       |
          2=black |
          ,       |   1=excellent,..., 5=poor  
          3=other |             8         Total
          --------+----------------------------
            White |           .13           100
                  | [.0646,.2616]              
                  | 
            Black |             0           100
                  |                            
                  | 
            Other |             0           100
                  |                            
                  | 
            Total |         .1143           100
                  | [.0575,.2272]              
          -------------------------------------
          Key: Row percentage
               [95% confidence interval for row percentage]
          
            Pearson:
              Uncorrected   chi2(10)        =  230.0873
              Design-based  F(1.72, 53.47)  =    4.1862     P = 0.0254
          (running tabulate on estimation sample)
          
          Number of strata = 31                            Number of obs   =      10,349
          Number of PSUs   = 62                            Population size = 117,131,111
                                                           Design df       =          31
          
          -------------------------------------------------------------------------------
          1=male, |
          2=femal |                        1=excellent,..., 5=poor                       
          e       |             1             2             3             4             5
          --------+----------------------------------------------------------------------
             Male |         29.94         27.32         27.01         10.66         5.015
                  |  [28.32,31.6] [25.58,29.12]  [25.5,28.56] [9.678,11.72] [4.162,6.031]
                  | 
           Female |         25.22         27.61         28.78         13.77         4.464
                  |  [23.24,27.3] [26.24,29.02] [27.25,30.36] [12.59,15.04] [3.699,5.378]
                  | 
            Total |         27.48         27.47         27.93         12.28         4.728
                  | [26.09,28.91] [26.38,28.58] [26.92,28.96] [11.41,13.19] [4.024,5.549]
          -------------------------------------------------------------------------------
          
          -------------------------------------
          1=male, |
          2=femal |   1=excellent,..., 5=poor  
          e       |             8         Total
          --------+----------------------------
             Male |         .0667           100
                  | [.0193,.2302]              
                  | 
           Female |         .1581           100
                  | [.0667,.3745]              
                  | 
            Total |         .1143           100
                  | [.0575,.2272]              
          -------------------------------------
          Key: Row percentage
               [95% confidence interval for row percentage]
          
            Pearson:
              Uncorrected   chi2(5)         =   47.8631
              Design-based  F(4.21, 130.60) =    6.7167     P = 0.0000
          
          . 
          . 
          . capture ssc install tabout, replace
          
          . 
          . * although -tabout- writes to text files only, 
          . * it's possible to later open the text file with Excel
          . * and get reasonably formatted tables 
          . *       (say "yes" when Excel asks whether you want to open the file)
          . * If the output file is being replaced, ensure that it's not open when
          . *   redoing your code
          . foreach var of varlist agegrp hlthstat {
            2.         forval x = 0/1 {
            3.                 tabout race sex `var' using `var'`x'.xls, ///
          >                         c(row ci) f(1 1) clab(Row_% 95%_CI) svy ///
          >                         stats(chi2) npos(lab) per replace
            4.         }
            5. }
          Survey results being calculated
          ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
          ..............
          Table output written to: agegrp0.xls
          
                  Age groups 1-6                                                         
          >                                  
                  age20-29        age20-29        age30-39        age30-39        age40-4
          > 9        age40-49        age50-59        age50-59        age60-69        age6
          > 0-69        age 70+ age 70+ Total
                  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %  
          >  95% CI  Row %   95% CI  Row %
          1=white, 2=black, 3=other                                                      
          >                                                  
          White (n=9,065) 27.4    [26.0,28.9]     20.3    [19.0,21.7]     16.9    [16.0,1
          > 7.9]     16.8    [15.7,18.1]     13.7    [12.9,14.6]     4.8     [4.2,5.5]   
          >     100.0
          Black (n=1,086) 32.9    [29.1,37.1]     21.4    [18.9,24.2]     15.8    [13.2,1
          > 8.9]     15.9    [13.6,18.5]     10.2    [8.7,11.9]      3.8     [2.8,5.0]   
          >     100.0
          Other (n=200)   31.2    [22.4,41.7]     20.6    [15.6,26.6]     18.1    [14.4,2
          > 2.5]     16.0    [11.5,21.7]     11.5    [5.7,21.9]      2.5     [1.0,6.1]   
          >     100.0
          Total (n=10,351)        28.0    [26.6,29.5]     20.4    [19.3,21.6]     16.8   
          >  [16.0,17.7]     16.7    [15.7,17.8]     13.3    [12.6,14.2]     4.6     [4.1
          > ,5.3]       100.0
          
          Pearson: Uncorrected chi2(10) = 26.4662                                        
          >                                                          
          Design-based F(3.89, 120.70) = 1.7503 Pr = 0.145                               
          >                                                                          
          
          1=male, 2=female                                                               
          >                                          
          Male (n=4,915)  28.3    [26.4,30.3]     21.0    [19.3,22.8]     17.0    [15.8,1
          > 8.2]     16.4    [15.0,17.9]     13.1    [12.1,14.2]     4.2     [3.6,4.8]   
          >     100.0
          Female (n=5,436)        27.8    [26.1,29.5]     19.9    [18.7,21.2]     16.7   
          >  [15.7,17.8]     17.0    [15.6,18.4]     13.6    [12.8,14.5]     5.0     [4.3
          > ,5.8]       100.0
          Total (n=10,351)        28.0    [26.6,29.5]     20.4    [19.3,21.6]     16.8   
          >  [16.0,17.7]     16.7    [15.7,17.8]     13.3    [12.6,14.2]     4.6     [4.1
          > ,5.3]       100.0
          
          Pearson: Uncorrected chi2(5) = 6.5876                                          
          >                                                          
          Design-based F(3.89, 120.53) = 1.2212 Pr = 0.306                               
          >                                                                          
          Survey results being calculated
          ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
          ..............
          Table output written to: agegrp1.xls
          
                  Age groups 1-6                                                         
          >                                  
                  age20-29        age20-29        age30-39        age30-39        age40-4
          > 9        age40-49        age50-59        age50-59        age60-69        age6
          > 0-69        age 70+ age 70+ Total
                  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %  
          >  95% CI  Row %   95% CI  Row %
          1=white, 2=black, 3=other                                                      
          >                                                  
          White (n=9,065) 27.4    [26.0,28.9]     20.3    [19.0,21.7]     16.9    [16.0,1
          > 7.9]     16.8    [15.7,18.1]     13.7    [12.9,14.6]     4.8     [4.2,5.5]   
          >     100.0
          Black (n=1,086) 32.9    [29.1,37.1]     21.4    [18.9,24.2]     15.8    [13.2,1
          > 8.9]     15.9    [13.6,18.5]     10.2    [8.7,11.9]      3.8     [2.8,5.0]   
          >     100.0
          Other (n=200)   31.2    [22.4,41.7]     20.6    [15.6,26.6]     18.1    [14.4,2
          > 2.5]     16.0    [11.5,21.7]     11.5    [5.7,21.9]      2.5     [1.0,6.1]   
          >     100.0
          Total (n=10,351)        28.0    [26.6,29.5]     20.4    [19.3,21.6]     16.8   
          >  [16.0,17.7]     16.7    [15.7,17.8]     13.3    [12.6,14.2]     4.6     [4.1
          > ,5.3]       100.0
          
          Pearson: Uncorrected chi2(10) = 26.4662                                        
          >                                                          
          Design-based F(3.89, 120.70) = 1.7503 Pr = 0.145                               
          >                                                                          
          
          1=male, 2=female                                                               
          >                                          
          Male (n=4,915)  28.3    [26.4,30.3]     21.0    [19.3,22.8]     17.0    [15.8,1
          > 8.2]     16.4    [15.0,17.9]     13.1    [12.1,14.2]     4.2     [3.6,4.8]   
          >     100.0
          Female (n=5,436)        27.8    [26.1,29.5]     19.9    [18.7,21.2]     16.7   
          >  [15.7,17.8]     17.0    [15.6,18.4]     13.6    [12.8,14.5]     5.0     [4.3
          > ,5.8]       100.0
          Total (n=10,351)        28.0    [26.6,29.5]     20.4    [19.3,21.6]     16.8   
          >  [16.0,17.7]     16.7    [15.7,17.8]     13.3    [12.6,14.2]     4.6     [4.1
          > ,5.3]       100.0
          
          Pearson: Uncorrected chi2(5) = 6.5876                                          
          >                                                          
          Design-based F(3.89, 120.53) = 1.2212 Pr = 0.306                               
          >                                                                          
          Survey results being calculated
          ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
          ..............
          Table output written to: hlthstat0.xls
          
                  1=excellent,..., 5=poor                                                
          >                                          
                  1       1       2       2       3       3       4       4       5      
          >  5       8       8       Total
                  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %  
          >  95% CI  Row %   95% CI  Row %
          1=white, 2=black, 3=other                                                      
          >                                                  
          White (n=9,063) 28.8    [27.4,30.3]     28.5    [27.4,29.5]     26.9    [25.7,2
          > 8.0]     11.6    [10.6,12.6]     4.1     [3.4,5.1]       0.1     [0.1,0.3]   
          >     100.0
          Black (n=1,086) 16.0    [12.8,19.8]     19.4    [16.6,22.6]     34.9    [32.1,3
          > 7.9]     19.7    [16.7,23.0]     10.0    [8.2,12.2]      0.0             100.
          > 0
          Other (n=200)   23.6    [15.6,34.2]     23.6    [16.5,32.5]     38.2    [30.0,4
          > 7.1]     9.4     [6.0,14.4]      5.2     [3.1,8.7]       0.0             100.
          > 0
          Total (n=10,349)        27.5    [26.1,28.9]     27.5    [26.4,28.6]     27.9   
          >  [26.9,29.0]     12.3    [11.4,13.2]     4.7     [4.0,5.5]       0.1     [0.1
          > ,0.2]       100.0
          
          Pearson: Uncorrected chi2(10) = 230.0873                                       
          >                                                                  
          Design-based F(1.72, 53.47) = 4.1862 Pr = 0.025                                
          >                                                                  
          
          1=male, 2=female                                                               
          >                                          
          Male (n=4,915)  29.9    [28.3,31.6]     27.3    [25.6,29.1]     27.0    [25.5,2
          > 8.6]     10.7    [9.7,11.7]      5.0     [4.2,6.0]       0.1     [0.0,0.2]   
          >     100.0
          Female (n=5,434)        25.2    [23.2,27.3]     27.6    [26.2,29.0]     28.8   
          >  [27.3,30.4]     13.8    [12.6,15.0]     4.5     [3.7,5.4]       0.2     [0.1
          > ,0.4]       100.0
          Total (n=10,349)        27.5    [26.1,28.9]     27.5    [26.4,28.6]     27.9   
          >  [26.9,29.0]     12.3    [11.4,13.2]     4.7     [4.0,5.5]       0.1     [0.1
          > ,0.2]       100.0
          
          Pearson: Uncorrected chi2(5) = 47.8631                                         
          >                                                          
          Design-based F(4.21, 130.60) = 6.7167 Pr = 0.000                               
          >                                                                          
          Survey results being calculated
          ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
          ..............
          Table output written to: hlthstat1.xls
          
                  1=excellent,..., 5=poor                                                
          >                                          
                  1       1       2       2       3       3       4       4       5      
          >  5       8       8       Total
                  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %  
          >  95% CI  Row %   95% CI  Row %
          1=white, 2=black, 3=other                                                      
          >                                                  
          White (n=9,063) 28.8    [27.4,30.3]     28.5    [27.4,29.5]     26.9    [25.7,2
          > 8.0]     11.6    [10.6,12.6]     4.1     [3.4,5.1]       0.1     [0.1,0.3]   
          >     100.0
          Black (n=1,086) 16.0    [12.8,19.8]     19.4    [16.6,22.6]     34.9    [32.1,3
          > 7.9]     19.7    [16.7,23.0]     10.0    [8.2,12.2]      0.0             100.
          > 0
          Other (n=200)   23.6    [15.6,34.2]     23.6    [16.5,32.5]     38.2    [30.0,4
          > 7.1]     9.4     [6.0,14.4]      5.2     [3.1,8.7]       0.0             100.
          > 0
          Total (n=10,349)        27.5    [26.1,28.9]     27.5    [26.4,28.6]     27.9   
          >  [26.9,29.0]     12.3    [11.4,13.2]     4.7     [4.0,5.5]       0.1     [0.1
          > ,0.2]       100.0
          
          Pearson: Uncorrected chi2(10) = 230.0873                                       
          >                                                                  
          Design-based F(1.72, 53.47) = 4.1862 Pr = 0.025                                
          >                                                                  
          
          1=male, 2=female                                                               
          >                                          
          Male (n=4,915)  29.9    [28.3,31.6]     27.3    [25.6,29.1]     27.0    [25.5,2
          > 8.6]     10.7    [9.7,11.7]      5.0     [4.2,6.0]       0.1     [0.0,0.2]   
          >     100.0
          Female (n=5,434)        25.2    [23.2,27.3]     27.6    [26.2,29.0]     28.8   
          >  [27.3,30.4]     13.8    [12.6,15.0]     4.5     [3.7,5.4]       0.2     [0.1
          > ,0.4]       100.0
          Total (n=10,349)        27.5    [26.1,28.9]     27.5    [26.4,28.6]     27.9   
          >  [26.9,29.0]     12.3    [11.4,13.2]     4.7     [4.0,5.5]       0.1     [0.1
          > ,0.2]       100.0
          
          Pearson: Uncorrected chi2(5) = 47.8631                                         
          >                                                          
          Design-based F(4.21, 130.60) = 6.7167 Pr = 0.000                               
          >                                                                          
          
          . 
          . collect clear
          
          . svy: tab race hlthstat, row percent ci 
          (running tabulate on estimation sample)
          
          Number of strata = 31                            Number of obs   =      10,349
          Number of PSUs   = 62                            Population size = 117,131,111
                                                           Design df       =          31
          
          -------------------------------------------------------------------------------
          1=white |
          ,       |
          2=black |
          ,       |                        1=excellent,..., 5=poor                       
          3=other |             1             2             3             4             5
          --------+----------------------------------------------------------------------
            White |         28.84         28.46         26.88         11.56          4.14
                  |  [27.42,30.3] [27.44,29.49] [25.75,28.03] [10.61,12.58] [3.359,5.093]
                  | 
            Black |         15.99         19.43          34.9         19.66         10.02
                  | [12.79,19.82]  [16.6,22.61] [32.06,37.86] [16.72,22.96] [8.157,12.25]
                  | 
            Other |         23.62         23.56         38.22         9.407         5.185
                  | [15.57,34.15] [16.48,32.51] [30.04,47.13] [6.003,14.44] [3.052,8.675]
                  | 
            Total |         27.48         27.47         27.93         12.28         4.728
                  | [26.09,28.91] [26.38,28.58] [26.92,28.96] [11.41,13.19] [4.024,5.549]
          -------------------------------------------------------------------------------
          
          -------------------------------------
          1=white |
          ,       |
          2=black |
          ,       |   1=excellent,..., 5=poor  
          3=other |             8         Total
          --------+----------------------------
            White |           .13           100
                  | [.0646,.2616]              
                  | 
            Black |             0           100
                  |                            
                  | 
            Other |             0           100
                  |                            
                  | 
            Total |         .1143           100
                  | [.0575,.2272]              
          -------------------------------------
          Key: Row percentage
               [95% confidence interval for row percentage]
          
            Pearson:
              Uncorrected   chi2(10)        =  230.0873
              Design-based  F(1.72, 53.47)  =    4.1862     P = 0.0254
          
          . collect get _r_b _r_ci
          
          . /* svy: tab sex hlthstat, row percent ci 
          > collect get _r_b _r_ci
          > svy: tab race agegrp, row percent ci 
          > collect get _r_b _r_ci
          > svy: tab sex agegrp, row percent ci 
          > collect get _r_b _r_ci
          > */
          . 
          . collect dir
          
          Collections in memory
          Current: default
          -------------------
             Name   No. items
          -------------------
          default   711      
          -------------------
          
          . collect dims
          
          Collection dimensions
          Collection: default
          -----------------------------------------
                             Dimension   No. levels
          -----------------------------------------
          Layout, style, header, label
                                cmdset   1         
                                 coleq   4         
                               colname   33        
                         program_class   1         
                                result   88        
                           result_type   3         
                                 roweq   3         
                               rowname   28        
          
          Style only
                          border_block   4         
                             cell_type   4         
          -----------------------------------------
          
          . 
          . * coefficient [lower_bound  upper_bound]
          . *       results for row 1, then row 2, etc
          . collect layout (colname) (result)
          
          Collection: default
                Rows: colname
             Columns: result
             Table 1: 18 x 2
          
          ------------------------------------
              | Coefficient    __LEVEL__% CI  
          ----+-------------------------------
          p11 |    .2883911 .2739682  .3028139
          p12 |    .2845682 .2743347  .2948016
          p13 |    .2687608 .2573412  .2801804
          p14 |    .1155795 .1056924  .1254667
          p15 |    .0414005 .0327802  .0500208
          p16 |    .0012999 .0003904  .0022094
          p21 |    .1599261 .1248707  .1949816
          p22 |    .1942849 .1642758  .2242941
          p23 |    .3490481 .3200471  .3780492
          p24 |    .1965652 .1653582  .2277721
          p25 |    .1001757 .0798108  .1205405
          p26 |           0                   
          p31 |     .236245 .1429917  .3294982
          p32 |    .2356336 .1552486  .3160185
          p33 |    .3822058 .2959553  .4684562
          p34 |    .0940669 .0526481  .1354856
          p35 |    .0518488 .0246995  .0789981
          p36 |           0                   
          ------------------------------------
          
          . * is same as: 
          . collect layout (colname) (result[_r_b _r_ci])
          
          Collection: default
                Rows: colname
             Columns: result[_r_b _r_ci]
             Table 1: 18 x 2
          
          ------------------------------------
              | Coefficient    __LEVEL__% CI  
          ----+-------------------------------
          p11 |    .2883911 .2739682  .3028139
          p12 |    .2845682 .2743347  .2948016
          p13 |    .2687608 .2573412  .2801804
          p14 |    .1155795 .1056924  .1254667
          p15 |    .0414005 .0327802  .0500208
          p16 |    .0012999 .0003904  .0022094
          p21 |    .1599261 .1248707  .1949816
          p22 |    .1942849 .1642758  .2242941
          p23 |    .3490481 .3200471  .3780492
          p24 |    .1965652 .1653582  .2277721
          p25 |    .1001757 .0798108  .1205405
          p26 |           0                   
          p31 |     .236245 .1429917  .3294982
          p32 |    .2356336 .1552486  .3160185
          p33 |    .3822058 .2959553  .4684562
          p34 |    .0940669 .0526481  .1354856
          p35 |    .0518488 .0246995  .0789981
          p36 |           0                   
          ------------------------------------
          
          . 
          . *  coefficient 
          . *  [lower_bound  upper_bound]
          . collect layout (colname##result)
          
          Collection: default
                Rows: colname##result
             Table 1: 55 x 1
          
          ------------------------------------
          White           |                  1
          Black           |                  2
          Other           |                  3
          p11             |                   
            Coefficient   |           .2883911
            __LEVEL__% CI | .2739682  .3028139
          p12             |                   
            Coefficient   |           .2845682
            __LEVEL__% CI | .2743347  .2948016
          p13             |                   
            Coefficient   |           .2687608
            __LEVEL__% CI | .2573412  .2801804
          p14             |                   
            Coefficient   |           .1155795
            __LEVEL__% CI | .1056924  .1254667
          p15             |                   
            Coefficient   |           .0414005
            __LEVEL__% CI | .0327802  .0500208
          p16             |                   
            Coefficient   |           .0012999
            __LEVEL__% CI | .0003904  .0022094
          p21             |                   
            Coefficient   |           .1599261
            __LEVEL__% CI | .1248707  .1949816
          p22             |                   
            Coefficient   |           .1942849
            __LEVEL__% CI | .1642758  .2242941
          p23             |                   
            Coefficient   |           .3490481
            __LEVEL__% CI | .3200471  .3780492
          p24             |                   
            Coefficient   |           .1965652
            __LEVEL__% CI | .1653582  .2277721
          p25             |                   
            Coefficient   |           .1001757
            __LEVEL__% CI | .0798108  .1205405
          p26             |                   
            Coefficient   |                  0
          p31             |                   
            Coefficient   |            .236245
            __LEVEL__% CI | .1429917  .3294982
          p32             |                   
            Coefficient   |           .2356336
            __LEVEL__% CI | .1552486  .3160185
          p33             |                   
            Coefficient   |           .3822058
            __LEVEL__% CI | .2959553  .4684562
          p34             |                   
            Coefficient   |           .0940669
            __LEVEL__% CI | .0526481  .1354856
          p35             |                   
            Coefficient   |           .0518488
            __LEVEL__% CI | .0246995  .0789981
          p36             |                   
            Coefficient   |                  0
          ------------------------------------
          Now for healthstat0.xls output from -tabout- (essentially what was echoed above)

          Code:
              1=excellent,..., 5=poor                                                
              1    1    2    2    3    3    4    4    5    5    8    8    Total
              Row %    95% CI    Row %    95% CI    Row %    95% CI    Row %    95% CI    Row %    95% CI    Row %    95% CI    Row %
          1=white, 2=black, 3=other                                                    
          White (n=9,063)    28.8    [27.4,30.3]    28.5    [27.4,29.5]    26.9    [25.7,28.0]    11.6    [10.6,12.6]    4.1    [3.4,5.1]    0.1    [0.1,0.3]    100.0
          Black (n=1,086)    16.0    [12.8,19.8]    19.4    [16.6,22.6]    34.9    [32.1,37.9]    19.7    [16.7,23.0]    10.0    [8.2,12.2]    0.0        100.0
          Other (n=200)    23.6    [15.6,34.2]    23.6    [16.5,32.5]    38.2    [30.0,47.1]    9.4    [6.0,14.4]    5.2    [3.1,8.7]    0.0        100.0
          Total (n=10,349)    27.5    [26.1,28.9]    27.5    [26.4,28.6]    27.9    [26.9,29.0]    12.3    [11.4,13.2]    4.7    [4.0,5.5]    0.1    [0.1,0.2]    100.0
          
          Pearson: Uncorrected chi2(10) = 230.0873                                                    
          Design-based F(1.72, 53.47) = 4.1862 Pr = 0.025                                                    
          
          1=male, 2=female                                                    
          Male (n=4,915)    29.9    [28.3,31.6]    27.3    [25.6,29.1]    27.0    [25.5,28.6]    10.7    [9.7,11.7]    5.0    [4.2,6.0]    0.1    [0.0,0.2]    100.0
          Female (n=5,434)    25.2    [23.2,27.3]    27.6    [26.2,29.0]    28.8    [27.3,30.4]    13.8    [12.6,15.0]    4.5    [3.7,5.4]    0.2    [0.1,0.4]    100.0
          Total (n=10,349)    27.5    [26.1,28.9]    27.5    [26.4,28.6]    27.9    [26.9,29.0]    12.3    [11.4,13.2]    4.7    [4.0,5.5]    0.1    [0.1,0.2]    100.0
          
          Pearson: Uncorrected chi2(5) = 47.8631                                                    
          Design-based F(4.21, 130.60) = 6.7167 Pr = 0.000

          Comment


          • #6
            As mentioned by Rich and Andrew, svy: tabulate now has options for better collect support in Stata 19.

            For example, using a single table from the above examples, let's try to get svy: tabulate with collect to reproduce tabout output.

            First, start with tabout
            Code:
            . webuse nhanes2l
            (Second National Health and Nutrition Examination Survey)
            
            . 
            . tabout race hlthstat using tabout.txt, ///
            >         c(row ci) ///
            >         f(1 1) ///
            >         clab(Row_% 95%_CI) ///
            >         svy ///
            >         stats(chi2) ///
            >         npos(lab) ///
            >         per ///
            >         replace
            Survey results being calculated
            ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
            .......
            Table output written to: tabout.txt
            
                    Health status                                                                           
            Race    Excellent       Excellent       Very good       Very good       Good    Good    Fair    Fair    Poor    Poor    Total
                    Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %   95% CI  Row %
            White (n=9,049) 28.9    [27.5,30.3]     28.5    [27.5,29.5]     26.9    [25.8,28.1]     11.6    [10.6,12.6]     4.1     [3.4,5.1]       100.0
            Black (n=1,086) 16.0    [12.8,19.8]     19.4    [16.6,22.6]     34.9    [32.1,37.9]     19.7    [16.7,23.0]     10.0    [8.2,12.2]      100.0
            Other (n=200)   23.6    [15.6,34.2]     23.6    [16.5,32.5]     38.2    [30.0,47.1]     9.4     [6.0,14.4]      5.2     [3.1,8.7]       100.0
            Total (n=10,335)        27.5    [26.1,28.9]     27.5    [26.4,28.6]     28.0    [27.0,29.0]     12.3    [11.4,13.2]     4.7     [4.0,5.6]       100.0
            
            Pearson: Uncorrected chi2(8) = 228.1808                                                                                 
            Design-based F(5.72, 177.30) = 16.5365 Pr = 0.000
            Now we call svy: tabulate with option collect.
            Code:
            . svy: tabulate race hlthstat, row ci percent pearson collect
            (running tabulate on estimation sample)
            
            Number of strata = 31                            Number of obs   =      10,335
            Number of PSUs   = 62                            Population size = 116,997,257
                                                             Design df       =          31
            
            ----------------------------------------------------------------------------------------------------
                      |                                      Health status                                      
                 Race |      Excellen       Very goo           Good           Fair           Poor          Total
            ----------+-----------------------------------------------------------------------------------------
                White |         28.88          28.49          26.91          11.57          4.145            100
                      | [27.46,30.34]  [27.48,29.53]  [25.78,28.07]   [10.62,12.6]    [3.363,5.1]               
                      | 
                Black |         15.99          19.43           34.9          19.66          10.02            100
                      | [12.79,19.82]   [16.6,22.61]  [32.06,37.86]  [16.72,22.96]  [8.157,12.25]               
                      | 
                Other |         23.62          23.56          38.22          9.407          5.185            100
                      | [15.57,34.15]  [16.48,32.51]  [30.04,47.13]  [6.003,14.44]  [3.052,8.675]               
                      | 
                Total |         27.51           27.5          27.96          12.29          4.733            100
                      | [26.13,28.94]  [26.42,28.61]     [26.95,29]  [11.43,13.21]  [4.028,5.555]               
            ----------------------------------------------------------------------------------------------------
            Key: Row percentage
                 [95% confidence interval for row percentage]
            
              Pearson:
                Uncorrected   chi2(8)         =  228.1808
                Design-based  F(5.72, 177.30) =   16.5365     P = 0.0000
            This output is not from collect. The collect option here specifies that the tabulated results are put in a collection named Tabulate, with its own styles and layout. The default collect layout tries to match the original output.
            Code:
            . collect layout
            
            Collection: Tabulate
                  Rows: race#result result
               Columns: hlthstat _overall
                Tables: cmdset#item_type
               Table 1: 13 x 6
               Table 2: 2 x 1
            
            ------------------------------------------------------------------------------------------------------
                                                                 Health status                                    
                                 Excellent       Very good            Good            Fair           Poor    Total
            ------------------------------------------------------------------------------------------------------
            Race                                                                                                  
              White                                                                                               
                Row percent          28.88           28.49           26.91           11.57           4.15   100.00
                95% CI       [27.46 30.34]   [27.48 29.53]   [25.78 28.07]   [10.62 12.60]    [3.36 5.10]         
              Black                                                                                               
                Row percent          15.99           19.43           34.90           19.66          10.02   100.00
                95% CI       [12.79 19.82]   [16.60 22.61]   [32.06 37.86]   [16.72 22.96]   [8.16 12.25]         
              Other                                                                                               
                Row percent          23.62           23.56           38.22            9.41           5.18   100.00
                95% CI       [15.57 34.15]   [16.48 32.51]   [30.04 47.13]    [6.00 14.44]    [3.05 8.68]         
              Total                                                                                               
                Row percent          27.51           27.50           27.96           12.29           4.73   100.00
                95% CI       [26.13 28.94]   [26.42 28.61]   [26.95 29.00]   [11.43 13.21]    [4.03 5.56]         
            ------------------------------------------------------------------------------------------------------
            
                                  Uncorrected Pearson χ²(8) = 228.1808
            Design-based Pearson F(5.72, 177.30) = 16.5365 P = <0.0001
            Note the presence of variables race and hlthstat in the layout. The other dimensions are also important for the layout, so we need to keep them in our future layout edits.

            Let's append a new table to our collection, using "row" variable sex. I'll highlight the differences in this second call to svy: tabulate
            Code:
            . svy: tabulate sex hlthstat, row ci percent pearson collect(, append)
            (running tabulate on estimation sample)
            
            Number of strata = 31                            Number of obs   =      10,335
            Number of PSUs   = 62                            Population size = 116,997,257
                                                             Design df       =          31
            
            ----------------------------------------------------------------------------------------------------
                      |                                      Health status                                      
                  Sex |      Excellen       Very goo           Good           Fair           Poor          Total
            ----------+-----------------------------------------------------------------------------------------
                 Male |         29.96          27.34          27.02          10.66          5.018            100
                      | [28.35,31.62]   [25.6,29.15]  [25.52,28.58]  [9.684,11.73]  [4.165,6.035]               
                      | 
               Female |         25.26          27.65          28.83          13.79          4.471            100
                      | [23.29,27.33]  [26.29,29.06]  [27.28,30.42]   [12.6,15.07]  [3.704,5.388]               
                      | 
                Total |         27.51           27.5          27.96          12.29          4.733            100
                      | [26.13,28.94]  [26.42,28.61]     [26.95,29]  [11.43,13.21]  [4.028,5.555]               
            ----------------------------------------------------------------------------------------------------
            Key: Row percentage
                 [95% confidence interval for row percentage]
            
              Pearson:
                Uncorrected   chi2(4)         =   45.9595
                Design-based  F(3.57, 110.77) =    8.0264     P = 0.0000
            Again the output from svy: tabulate is unaffected by the collect() option; however, with suboption append we specify that this table's results are put/appended into our collection. Note that the layout remains unchanged, so we will need to update it to add the new "row" variable, and arrange the results to look more like tabout.

            First let's add sex to the layout.
            Code:
            . collect layout ((race sex)#result result) (hlthstat _overall) (cmdset#item_type)
            
            Collection: Tabulate
                  Rows: (race sex)#result result
               Columns: hlthstat _overall
                Tables: cmdset#item_type
               Table 1: 13 x 6
               Table 2: 2 x 1
               Table 3: 10 x 6
               Table 4: 2 x 1
            
            ------------------------------------------------------------------------------------------------------
                                                                 Health status                                    
                                 Excellent       Very good            Good            Fair           Poor    Total
            ------------------------------------------------------------------------------------------------------
            Race                                                                                                  
              White                                                                                               
                Row percent          28.88           28.49           26.91           11.57           4.15   100.00
                95% CI       [27.46 30.34]   [27.48 29.53]   [25.78 28.07]   [10.62 12.60]    [3.36 5.10]         
              Black                                                                                               
                Row percent          15.99           19.43           34.90           19.66          10.02   100.00
                95% CI       [12.79 19.82]   [16.60 22.61]   [32.06 37.86]   [16.72 22.96]   [8.16 12.25]         
              Other                                                                                               
                Row percent          23.62           23.56           38.22            9.41           5.18   100.00
                95% CI       [15.57 34.15]   [16.48 32.51]   [30.04 47.13]    [6.00 14.44]    [3.05 8.68]         
              Total                                                                                               
                Row percent          27.51           27.50           27.96           12.29           4.73   100.00
                95% CI       [26.13 28.94]   [26.42 28.61]   [26.95 29.00]   [11.43 13.21]    [4.03 5.56]         
            ------------------------------------------------------------------------------------------------------
            
                                  Uncorrected Pearson χ²(8) = 228.1808
            Design-based Pearson F(5.72, 177.30) = 16.5365 P = <0.0001
            
            -----------------------------------------------------------------------------------------------------
                                                                 Health status                                   
                                 Excellent       Very good            Good            Fair          Poor    Total
            -----------------------------------------------------------------------------------------------------
            Sex                                                                                                  
              Male                                                                                               
                Row percent          29.96           27.34           27.02           10.66          5.02   100.00
                95% CI       [28.35 31.62]   [25.60 29.15]   [25.52 28.58]    [9.68 11.73]   [4.17 6.04]         
              Female                                                                                             
                Row percent          25.26           27.65           28.83           13.79          4.47   100.00
                95% CI       [23.29 27.33]   [26.29 29.06]   [27.28 30.42]   [12.60 15.07]   [3.70 5.39]         
              Total                                                                                              
                Row percent          27.51           27.50           27.96           12.29          4.73   100.00
                95% CI       [26.13 28.94]   [26.42 28.61]   [26.95 29.00]   [11.43 13.21]   [4.03 5.56]         
            -----------------------------------------------------------------------------------------------------
            
                                  Uncorrected Pearson χ²(4) = 45.9595
            Design-based Pearson F(3.57, 110.77) = 8.0264 P = <0.0001
            Now tabout reports the estimated percent values and their CIs in the same row instead of stacking them vertically. The simplest way to get collect to do this is to use composite results.
            Code:
            collect composite define ci = _r_lb _r_ub, trim delimiter(", ")
            collect style cell result[ci], sformat("[%s]")
            collect composite define both = row_percent ci
            collect style cell result[both], nformat(%9.1f)
            collect style header result[both], level(hide)
            collect layout ((race sex)#result[both] result) (hlthstat _overall) (cmdset#item_type)
            Here are the resulting tables.
            Code:
            -----------------------------------------------------------------------------------------------------------------
                                                                   Health status                                             
                             Excellent           Very good                Good                Fair               Poor   Total
            -----------------------------------------------------------------------------------------------------------------
            Race                                                                                                             
              White  28.9 [27.5, 30.3]   28.5 [27.5, 29.5]   26.9 [25.8, 28.1]   11.6 [10.6, 12.6]     4.1 [3.4, 5.1]   100.0
              Black  16.0 [12.8, 19.8]   19.4 [16.6, 22.6]   34.9 [32.1, 37.9]   19.7 [16.7, 23.0]   10.0 [8.2, 12.2]   100.0
              Other  23.6 [15.6, 34.2]   23.6 [16.5, 32.5]   38.2 [30.0, 47.1]     9.4 [6.0, 14.4]     5.2 [3.1, 8.7]   100.0
              Total  27.5 [26.1, 28.9]   27.5 [26.4, 28.6]   28.0 [27.0, 29.0]   12.3 [11.4, 13.2]     4.7 [4.0, 5.6]   100.0
            -----------------------------------------------------------------------------------------------------------------
            
                                  Uncorrected Pearson χ²(8) = 228.1808
            Design-based Pearson F(5.72, 177.30) = 16.5365 P = <0.0001
            
            ----------------------------------------------------------------------------------------------------------------
                                                                   Health status                                            
                              Excellent           Very good                Good                Fair             Poor   Total
            ----------------------------------------------------------------------------------------------------------------
            Sex                                                                                                             
              Male    30.0 [28.3, 31.6]   27.3 [25.6, 29.1]   27.0 [25.5, 28.6]    10.7 [9.7, 11.7]   5.0 [4.2, 6.0]   100.0
              Female  25.3 [23.3, 27.3]   27.7 [26.3, 29.1]   28.8 [27.3, 30.4]   13.8 [12.6, 15.1]   4.5 [3.7, 5.4]   100.0
              Total   27.5 [26.1, 28.9]   27.5 [26.4, 28.6]   28.0 [27.0, 29.0]   12.3 [11.4, 13.2]   4.7 [4.0, 5.6]   100.0
            ----------------------------------------------------------------------------------------------------------------
            
                                  Uncorrected Pearson χ²(4) = 45.9595
            Design-based Pearson F(3.57, 110.77) = 8.0264 P = <0.0001
            The only thing left here is to get the sample sizes into the row headers. There is not easy/built-in way to accomplish this. We need to loop over the levels of each row variable and update their labels in the collection. Here is what I came up with, accommodating for possible string "row" variables.
            Code:
            foreach var of varlist race sex {
                local typ : type `var'
                local isStr = substr("`typ'",1,3) == "str"
                collect label list `var'
                local k = s(k)
                forval i = 1/`k' {
                    local lev = s(level`i')
                    local lab = s(label`i')
                    if `"`lev'"' == "__margCode__" {
                        count if !missing(hlthstat)
                    }
                    else if `isStr' {
                        count if `var' == `"`lev'"' & !missing(hlthstat)
                    }
                    else {
                        count if `var' == `lev' & !missing(hlthstat)
                    }
                    local nobs : display %20.0fc r(N)
                    local nobs : list retok nobs
                    local update `"`update' "`lev'" `"`lab' (n=`nobs')"'"'
                }
                collect label levels `var' `update', modify
            }
            collect preview
            Here is the final output.
            Code:
            ----------------------------------------------------------------------------------------------------------------------------
                                                                              Health status                                             
                                        Excellent           Very good                Good                Fair               Poor   Total
            ----------------------------------------------------------------------------------------------------------------------------
            Race                                                                                                                        
              White (n=9,049)   28.9 [27.5, 30.3]   28.5 [27.5, 29.5]   26.9 [25.8, 28.1]   11.6 [10.6, 12.6]     4.1 [3.4, 5.1]   100.0
              Black (n=1,086)   16.0 [12.8, 19.8]   19.4 [16.6, 22.6]   34.9 [32.1, 37.9]   19.7 [16.7, 23.0]   10.0 [8.2, 12.2]   100.0
              Other (n=200)     23.6 [15.6, 34.2]   23.6 [16.5, 32.5]   38.2 [30.0, 47.1]     9.4 [6.0, 14.4]     5.2 [3.1, 8.7]   100.0
              Total (n=10,335)  27.5 [26.1, 28.9]   27.5 [26.4, 28.6]   28.0 [27.0, 29.0]   12.3 [11.4, 13.2]     4.7 [4.0, 5.6]   100.0
            ----------------------------------------------------------------------------------------------------------------------------
            
                                  Uncorrected Pearson χ²(8) = 228.1808
            Design-based Pearson F(5.72, 177.30) = 16.5365 P = <0.0001
            
            --------------------------------------------------------------------------------------------------------------------------
                                                                             Health status                                            
                                        Excellent           Very good                Good                Fair             Poor   Total
            --------------------------------------------------------------------------------------------------------------------------
            Sex                                                                                                                       
              Male (n=4,909)    30.0 [28.3, 31.6]   27.3 [25.6, 29.1]   27.0 [25.5, 28.6]    10.7 [9.7, 11.7]   5.0 [4.2, 6.0]   100.0
              Female (n=5,426)  25.3 [23.3, 27.3]   27.7 [26.3, 29.1]   28.8 [27.3, 30.4]   13.8 [12.6, 15.1]   4.5 [3.7, 5.4]   100.0
              Total (n=10,335)  27.5 [26.1, 28.9]   27.5 [26.4, 28.6]   28.0 [27.0, 29.0]   12.3 [11.4, 13.2]   4.7 [4.0, 5.6]   100.0
            --------------------------------------------------------------------------------------------------------------------------
            
                                  Uncorrected Pearson χ²(4) = 45.9595
            Design-based Pearson F(3.57, 110.77) = 8.0264 P = <0.0001
            Now we can use collect export to publish these tables to an Excel or Word document.

            For "column" variable agegrp, we repeat the above actions using agegrp in place of hltstat.

            Comment


            • #7
              Jeff Pitblado (StataCorp) This is terrific, Jeff -- many thanks from my friend -- and me. A great Christmans present. In addition to the solution, your careful exegsis is helpful with our learning.

              Andrew Masau -- many thanks as well. (Clearly, I missed the additional features of -collect- in Stata 19 that you referred to)
              Last edited by Stephen Jenkins; 21 Dec 2025, 09:48.

              Comment

              Working...
              X