Announcement

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

  • tabout (or similar) to show freq, unweighted %, & weighted %?

    Hello, I'm working with a survey dataset where we've been using tabout to get some basic one-way and two-way tables of the data. Having no trouble getting frequencies and %s, as any tab command would do. I can also get frequencies and weighted %s. But is it possible to get tabout to make freqs and both unweighted and weighted %s to show up in the columns? Or asdoc or another command? (Both commands are new to me, but my colleague is familiar with tabout.)

    Example of what I would want for output:
    ColVar
    n Unweighted% Weighted%
    Race1
    Race2
    Race3
    Thanks!

  • #2
    If you have Stata 18, you can use the new dtable command to produce customizable tables for your survey data. Here is an example of both types of tables, showing weighted and raw frequencies and percentages using some survey data.
    Code:
    . webuse nhanes2l
    (Second National Health and Nutrition Examination Survey)
    
    . svyset
    
    Sampling weights: finalwgt
                 VCE: linearized
         Single unit: missing
            Strata 1: strata
     Sampling unit 1: psu
               FPC 1: <zero>
    
    . 
    . dtable i.agegrp, svy ///
    >         sample(, statistics(frequency sumw)) ///
    >         factor(, statistics(fvrawfrequency fvrawpercent fvfrequency fvpercent))
    
    ------------------------------------------
                           Summary            
    ------------------------------------------
    N                       10,351 117,157,513
    Age group                                 
      20–29   2,320 (22.4%) 32,857,697 (28.0%)
      30–39   1,622 (15.7%) 23,935,443 (20.4%)
      40–49   1,272 (12.3%) 19,725,067 (16.8%)
      50–59   1,291 (12.5%) 19,584,095 (16.7%)
      60–69   2,860 (27.6%) 15,639,265 (13.3%)
      70+          986 (9.5%) 5,415,946 (4.6%)
    ------------------------------------------
    
    . 
    . dtable i.agegrp, svy ///
    >         by(sex) ///
    >         sample(, statistics(frequency sumw)) ///
    >         factor(, statistics(fvrawfrequency fvrawpercent fvfrequency fvpercent))
    
    ------------------------------------------------------------------------------------------------------------
                                                              Sex                                               
                            Male                            Female                            Total             
    ------------------------------------------------------------------------------------------------------------
    N                         4,915 56,159,480                 5,436 60,998,033               10,351 117,157,513
    Age group                                                                                                   
      20–29   1,116 (22.7%) 15,918,998 (28.3%) 1,204 (22.1%) 16,938,699 (27.8%) 2,320 (22.4%) 32,857,697 (28.0%)
      30–39     770 (15.7%) 11,777,252 (21.0%)   852 (15.7%) 12,158,191 (19.9%) 1,622 (15.7%) 23,935,443 (20.4%)
      40–49      610 (12.4%) 9,538,701 (17.0%)   662 (12.2%) 10,186,366 (16.7%) 1,272 (12.3%) 19,725,067 (16.8%)
      50–59      602 (12.2%) 9,219,958 (16.4%)   689 (12.7%) 10,364,137 (17.0%) 1,291 (12.5%) 19,584,095 (16.7%)
      60–69    1,369 (27.9%) 7,349,258 (13.1%)  1,491 (27.4%) 8,290,007 (13.6%) 2,860 (27.6%) 15,639,265 (13.3%)
      70+          448 (9.1%) 2,355,313 (4.2%)      538 (9.9%) 3,060,633 (5.0%)      986 (9.5%) 5,415,946 (4.6%)
    ------------------------------------------------------------------------------------------------------------
    end of do-file

    Comment


    • #3
      If you have Stata 17, you can use table to get similar tables with some
      style changes.
      Code:
      . webuse nhanes2l
      (Second National Health and Nutrition Examination Survey)
      
      . svyset
      
      Sampling weights: finalwgt
                   VCE: linearized
           Single unit: missing
              Strata 1: strata
       Sampling unit 1: psu
                 FPC 1: <zero>
      
      . 
      . table agegrp [pw=finalwgt], ///
      >         statistic(frequency) ///
      >         statistic(rawpercent) ///
      >         statistic(sumw) ///
      >         statistic(percent)
      
      ----------------------------------------------------------------------
                |  Frequency   Unweighted percent   Sum of weights   Percent
      ----------+-----------------------------------------------------------
      Age group |                                                           
        20–29   |      2,320                22.41       32,857,697     28.05
        30–39   |      1,622                15.67       23,935,443     20.43
        40–49   |      1,272                12.29       19,725,067     16.84
        50–59   |      1,291                12.47       19,584,095     16.72
        60–69   |      2,860                27.63       15,639,265     13.35
        70+     |        986                 9.53        5,415,946      4.62
        Total   |     10,351               100.00      117,157,513    100.00
      ----------------------------------------------------------------------
      
      . 
      . * style like dtable
      . collect style cell result[rawpercent percent] , nformat(%9.1f) sformat("(%s%%)")
      
      . collect composite define combo = frequency rawpercent sumw percent, trim
      
      . collect label levels result combo "Summary"
      
      . collect style cell border_block[corner row-header], border(right, pattern(none))
      
      . collect layout (agegrp) (result[combo])
      
      Collection: Table
            Rows: agegrp
         Columns: result[combo]
         Table 1: 8 x 1
      
      -----------------------------------------------
                                              Summary
      -----------------------------------------------
      Age group                                      
        20–29        2,320 (22.4%) 32,857,697 (28.0%)
        30–39        1,622 (15.7%) 23,935,443 (20.4%)
        40–49        1,272 (12.3%) 19,725,067 (16.8%)
        50–59        1,291 (12.5%) 19,584,095 (16.7%)
        60–69        2,860 (27.6%) 15,639,265 (13.3%)
        70+               986 (9.5%) 5,415,946 (4.6%)
        Total    10,351 (100.0%) 117,157,513 (100.0%)
      -----------------------------------------------
      
      . 
      . table agegrp sex [pw=finalwgt], ///
      >         statistic(frequency) ///
      >         statistic(rawpercent) ///
      >         statistic(sumw) ///
      >         statistic(percent)
      
      ---------------------------------------------------------------
                             |                   Sex                 
                             |        Male       Female         Total
      -----------------------+---------------------------------------
      Age group              |                                       
        20–29                |                                       
          Frequency          |       1,116        1,204         2,320
          Unweighted percent |       10.78        11.63         22.41
          Sum of weights     |  15,918,998   16,938,699    32,857,697
          Percent            |       13.59        14.46         28.05
        30–39                |                                       
          Frequency          |         770          852         1,622
          Unweighted percent |        7.44         8.23         15.67
          Sum of weights     |  11,777,252   12,158,191    23,935,443
          Percent            |       10.05        10.38         20.43
        40–49                |                                       
          Frequency          |         610          662         1,272
          Unweighted percent |        5.89         6.40         12.29
          Sum of weights     |   9,538,701   10,186,366    19,725,067
          Percent            |        8.14         8.69         16.84
        50–59                |                                       
          Frequency          |         602          689         1,291
          Unweighted percent |        5.82         6.66         12.47
          Sum of weights     |   9,219,958   10,364,137    19,584,095
          Percent            |        7.87         8.85         16.72
        60–69                |                                       
          Frequency          |       1,369        1,491         2,860
          Unweighted percent |       13.23        14.40         27.63
          Sum of weights     |   7,349,258    8,290,007    15,639,265
          Percent            |        6.27         7.08         13.35
        70+                  |                                       
          Frequency          |         448          538           986
          Unweighted percent |        4.33         5.20          9.53
          Sum of weights     |   2,355,313    3,060,633     5,415,946
          Percent            |        2.01         2.61          4.62
        Total                |                                       
          Frequency          |       4,915        5,436        10,351
          Unweighted percent |       47.48        52.52        100.00
          Sum of weights     |  56,159,480   60,998,033   117,157,513
          Percent            |       47.94        52.06        100.00
      ---------------------------------------------------------------
      
      . 
      . * style like dtable
      . collect style cell result[rawpercent percent] , nformat(%9.1f) sformat("(%s%%)")
      
      . collect composite define combo = frequency rawpercent sumw percent, trim
      
      . collect style header result, title(hide) level(hide)
      
      . collect style cell border_block[corner row-header], border(right, pattern(none))
      
      . collect layout (agegrp) (sex#result[combo])
      
      Collection: Table
            Rows: agegrp
         Columns: sex#result[combo]
         Table 1: 8 x 3
      
      ---------------------------------------------------------------------------------------------------------------------
                                                                     Sex                                                   
                                             Male                             Female                                  Total
      ---------------------------------------------------------------------------------------------------------------------
      Age group                                                                                                            
        20–29    1,116 (10.8%) 15,918,998 (13.6%)   1,204 (11.6%) 16,938,699 (14.5%)       2,320 (22.4%) 32,857,697 (28.0%)
        30–39       770 (7.4%) 11,777,252 (10.1%)      852 (8.2%) 12,158,191 (10.4%)       1,622 (15.7%) 23,935,443 (20.4%)
        40–49         610 (5.9%) 9,538,701 (8.1%)       662 (6.4%) 10,186,366 (8.7%)       1,272 (12.3%) 19,725,067 (16.8%)
        50–59         602 (5.8%) 9,219,958 (7.9%)       689 (6.7%) 10,364,137 (8.8%)       1,291 (12.5%) 19,584,095 (16.7%)
        60–69      1,369 (13.2%) 7,349,258 (6.3%)     1,491 (14.4%) 8,290,007 (7.1%)       2,860 (27.6%) 15,639,265 (13.3%)
        70+           448 (4.3%) 2,355,313 (2.0%)        538 (5.2%) 3,060,633 (2.6%)            986 (9.5%) 5,415,946 (4.6%)
        Total    4,915 (47.5%) 56,159,480 (47.9%)   5,436 (52.5%) 60,998,033 (52.1%)   10,351 (100.0%) 117,157,513 (100.0%)
      ---------------------------------------------------------------------------------------------------------------------

      Comment


      • #4
        Jeff, thank you so much! I'm confident one or both of these will work!

        Comment


        • #5
          Would you please also help with some code to then export the table results to excel or csv?

          Comment


          • #6
            In Stata type help collect export or click here.

            Comment


            • #7
              I got that figured out. Thanks. Now, is there a way I can have Stata 18 create a bunch of tables with categorical variables, still showing the freq, raw%, and weighted%?

              A program, foreach, forvalue, an array?

              Still using the nhanes21 dataset, this code works:

              .global varse "i.agegrp i.race i.hlthstat"

              . dtable $varse , svy by(sex) sample (, statistics(frequency sumw)) factor(, statistics (fvrawfrequency fvrawpercent fvpercent))

              But I have hundreds of categorical variables that I want to look at this way. How do I get around typing i.varname for each of hundreds of variables?

              Comment

              Working...
              X