Announcement

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

  • Row Percentages and Exporting Tables using SVY suite of commands

    Hi everyone, I have a question that is two-fold.

    I am using the svy suite of commands in Stata 18. I have a huge survey dataset that includes over 100 variables. I am trying to create tables that contain each of the 17 variables listed below in local vars (age4_new through ruralurban) stratfied by the other survey variables. I want the localvars in the rows and the other variable in the columns. The code below includes an example of one of the other (column) variables called "whyunins." The code below produces multiple tables (e.g. age4_new x whyuins, race x whyunins) and my first question is this: Is there a different coding process that I could use to create one large table with all the local vars on the rows and the other variable (whyunins, in this case) in the columns?

    My second question is this: How do I export the results with the row percentages to Excel once I have the output? I have already output the column percentage tables using the dtable command and it was easy enough to use "collect" to export the results to Excel. However, I cannot figure out how to use the collect function to export ROW percentages using the svy commands. I have tried many ways and, while collect will output an Excel spreadsheet, the spreadsheet is empty.


    local vars age4_new race hispanic gender sexor maritalstatus edu income ///
    fulltime parttime retired student employment_other selfemployed ///
    unemployed party ruralurban

    foreach var of local vars {
    svy: tab `var' whyunins, row count
    }


    Any guidance is appreciated! Thank you!

  • #2
    svy:tab was updated at some point to include -collect- as an option. Not sure if this was available in version 18. In any case, update your installation to the latest updates for that version.

    Code:
    update all
    In version 19.5, I can do the following:


    Code:
    webuse nhanes2b, clear
    collect clear
    
    local vars race sex region
    foreach var of local vars{
        svy: tabulate `var' diabetes, row collect(, append)
    }
    collect style header result[row_proportion], level(hide)
    collect layout ((`vars')#result) (diabetes[0 1])
    collect export mytable, as(xls) replace

    Res.:

    Code:
    . collect layout ((`vars')#result) (diabetes[0 1])
    
    Collection: Tabulate
          Rows: (race sex region)#result
       Columns: diabetes[0 1]
       Table 1: 15 x 2
    
    ---------------------------------
                  Diabetes status    
              Not diabetic   Diabetic
    ---------------------------------
    Race                            
      White         0.9680     0.0320
      Black         0.9410     0.0590
      Other         0.9797     0.0203
      Total         0.9658     0.0342
    Sex                              
      Male          0.9709     0.0291
      Female        0.9611     0.0389
      Total         0.9658     0.0342
    Region                          
      NE            0.9686     0.0314
      MW            0.9663     0.0337
      S             0.9590     0.0410
      W             0.9696     0.0304
      Total         0.9658     0.0342
    ---------------------------------
    Click image for larger version

Name:	Screenshot 2026-09-03 205450.png
Views:	1
Size:	24.3 KB
ID:	1787225

    Last edited by Andrew Musau; 03 Sep 2026, 12:58.

    Comment

    Working...
    X