Announcement

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

  • dtable Stata18 - Row percentages

    Hello!

    I am using dtable which provides column percentages, however, I also want to get row percentages. Do you know any way I can do that without doing, for example, this, dtable i.nonresponse, by(sex), for all my variables separately and then filling my table manually? I have 10 variables in total, separated by nonresponse (binary variable).

    Thank you!


  • #2

    I'll use the NHANES II data to show how to use table to get
    tables similar to dtable but with row percentages across a common
    by variable instead of column percentages within levels of the
    by variable.

    In the following I'm using rural as the binary by variable.
    The other variables in local macro vlist are the factor variable you
    would normally use with dtable to get column percentages.
    Code:
    webuse nhanes2l
    
    unab vlist : smsa sex race hlthstat heartatk diabetes highlead
    
    * build the overall sample table
    table () (rural), ///
        stat(frequency) ///
        stat(percent, across(rural)) ///
        name(N)
    collect addtags N[_hide]
    collect layout (N) (rural#result)
    
    * loop over the factor variables to compute their "row" percentages
    * across the levels of -rural-
    foreach v of local vlist {
        table (`v') (rural), ///
            stat(frequency) ///
            stat(percent, across(rural)) ///
            totals(`v') ///
            name(`v')
    }
    
    * create a new collection named 'all' that combines the above collections
    collect combine all = N `vlist'
    
    * define a composite result like -dtable- does
    collect composite define stats = frequency percent
    
    * add formats for the percent like -dtable- does
    collect style cell result[percent], nformat("%6.2f") sformat("(%s%%)")
    
    * hide the title and level labels in the header for our composite result
    collect style header result[stats], title(hide) level(hide)
    
    * new layout putting all the factor variables in the rows
    collect layout (N `vlist') (rural#result[stats])
    Here is the resulting table.
    Code:
    . collect layout (N `vlist') (rural#result[stats])
    
    Collection: all
          Rows: N smsa sex race hlthstat heartatk diabetes highlead
       Columns: rural#result[stats]
       Table 1: 27 x 3
    
    ------------------------------------------------------------------------------
                             |                         Rural                      
                             |           Urban            Rural              Total
    -------------------------+----------------------------------------------------
    N                        |  6,548 (63.26%)   3,803 (36.74%)   10,351 (100.00%)
    SMSA type                |                                                    
      SMSA, central city     |  2,629 (99.89%)        3 (0.11%)    2,632 (100.00%)
      SMSA, not central city |  2,360 (78.46%)     648 (21.54%)    3,008 (100.00%)
      Not in an SMSA         |  1,559 (33.09%)   3,152 (66.91%)    4,711 (100.00%)
    Sex                      |                                                    
      Male                   |  3,023 (61.51%)   1,892 (38.49%)    4,915 (100.00%)
      Female                 |  3,525 (64.85%)   1,911 (35.15%)    5,436 (100.00%)
    Race                     |                                                    
      White                  |  5,419 (59.78%)   3,646 (40.22%)    9,065 (100.00%)
      Black                  |    968 (89.13%)     118 (10.87%)    1,086 (100.00%)
      Other                  |    161 (80.50%)      39 (19.50%)      200 (100.00%)
    Health status            |                                                    
      Excellent              |  1,609 (66.85%)     798 (33.15%)    2,407 (100.00%)
      Very good              |  1,713 (66.11%)     878 (33.89%)    2,591 (100.00%)
      Good                   |  1,878 (63.92%)   1,060 (36.08%)    2,938 (100.00%)
      Fair                   |    950 (56.89%)     720 (43.11%)    1,670 (100.00%)
      Poor                   |    389 (53.36%)     340 (46.64%)      729 (100.00%)
    Prior heart attack       |                                                    
      No heart attack        |  6,272 (63.53%)   3,601 (36.47%)    9,873 (100.00%)
      Had heart attack       |    275 (57.77%)     201 (42.23%)      476 (100.00%)
    Diabetes status          |                                                    
      Not diabetic           |  6,233 (63.28%)   3,617 (36.72%)    9,850 (100.00%)
      Diabetic               |    314 (62.93%)     185 (37.07%)      499 (100.00%)
    High lead level          |                                                    
      lead<25                |  2,902 (62.34%)   1,753 (37.66%)    4,655 (100.00%)
      lead>=25               |    209 (71.33%)      84 (28.67%)      293 (100.00%)
    ------------------------------------------------------------------------------

    Comment


    • #3
      For the mentioned task, both dtable and asdocx are excellent choices. While dtable allows for the flexibility of programming your table to your specific needs, asdocx offers simplicity and effectiveness (having fee of $9.99). The table you mentioned can be easily reproduced using the template(table1) feature in asdocx.

      Code:
      asdocx tab rural i.smsa i.sex i.race i.hlthstat i.heartatk i.diabetes i.highlead, ///
           template(table1) row replace save(table.tex)
      Click image for larger version

Name:	Table1 asdocx.PNG
Views:	1
Size:	316.9 KB
ID:	1725006


      Export to Excel?
      Just change the file extension to .xlsx
      Code:
      asdocx tab rural i.smsa i.sex i.race i.hlthstat i.heartatk i.diabetes i.highlead, ///
           template(table1) row replace save(table.xlsx)
      Similarly, you can export to Word (.docx) or HTML (.html) format. To know more about asdocx, you may like to visit https://asdocx.com
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Jeff Pitblado (StataCorp) when using the survey data often time we need to use 'svy" could you please share the edits need for that ? Also, is it possible to get row proportion and standard error instead of confidence interval?

        Comment


        • #5
          Please see my post in this thread.

          Comment

          Working...
          X