Announcement

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

  • Export output from svy:tab using estout

    Hi All,

    I am working with survey data and I am interested in exporting the % of respondents who answered yes to having exercised in the past week. From previous posts, I saw that -estout- is a good option for this and I managed to extract the whole output to a csv file:

    svyset psu [pw = wgt], strata(v_stratum2)
    estpost svy: tab current, percent
    esttab . using "test.csv", b(2) se(2) nostar unstack replace

    This export the % (se) for all answers (yes, no), total and N. Instead I would only like to pick the % for those who said yes as I am exporting this value for several years to look at changes over time. Is there a way to only specify I want to export the one value or is there another approach more suitable to achieve my goal.

    Thanks!

  • #2
    estout is from the Stata Journal/ SSC (FAQ Advice #12). Something like?

    Code:
    webuse nhanes2b
    svyset psuid [pweight=finalwgt], strata(stratid)
    estpost svy: tab race, percent
    esttab . , keep(Black) noobs not nonotes nonumbers mlab("Percent") nostar
    Res.:

    Code:
    . estpost svy: tab race, percent
    (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,  |
    3=other   | percentage
    ----------+-----------
        White |      87.92
        Black |      9.551
        Other |      2.534
              | 
        Total |        100
    ----------------------
      Key:  percentage  =  cell percentage
    
    saved vectors:
                 e(b) =  cell percentages
                e(se) =  standard errors of cell percentages
                e(lb) =  lower 95% confidence bounds for cell percentages
                e(ub) =  upper 95% confidence bounds for cell percentages
              e(deff) =  deff for variances of cell percentages
              e(deft) =  deft for variances of cell percentages
              e(cell) =  cell percentages
             e(count) =  weighted counts
               e(obs) =  number of observations
    
    
    
    . esttab . , keep(Black) noobs not nonotes nonumbers mlab("Percent") nostar
    
    -------------------------
                      Percent
    -------------------------
    Black               9.551
    -------------------------
    
    .

    Comment


    • #3
      Thanks Andrew Musau , this is exactly what I wanted.

      As a follow-up, is there a way to specify which cell within the csv file to export the information to? Based on the help file, I am assuming there is no way to have several sheets within the csv by specifying the name of the sheet to be saved in as done when using -tab2xl-, which is sadly not supported by -svy-

      Comment


      • #4
        From #1, it appears that you are running a number of tabulations and saving a specific coefficient. How do you want the final file to look like? Do you want to have the coefficients stacked? It should be possible to offer a general solution if you clarify this.

        Comment


        • #5
          Hi Andrew Musau ; Yes so I would like to extract one coefficient from each Stata file and export it into a .csv (or Excel file) to have them all stacked. The extracted file would ideally have 2 variables, one indicating the year of the survey and the other the % of yes answers. Because of the way esttab works, there may be 3 columns year, yes, and percent as it seems to export the label of the category of interest. It should look something like:

          Year category Percent
          2015 yes 35
          2016 yes 37
          etc.



          Comment


          • #6
            If everything is in one file, you can loop. Otherwise, here is a technique assuming that the coefficient of interest has the label "yes". Modify if needed. If you have a variable year in each file, you can define the local "year" below as

            Code:
            local year= year[1]
            and

            Code:
            local year `year' `=year[1]'
            to save you from manually specifying the year. The matrix solution works here as matrices are maintained throughout the session, unless you clear them.


            Code:
            webuse nhanes2b, clear
            svyset psuid [pweight=finalwgt], strata(stratid)
            gen year= runiformint(2019, 2021)
            gen hbp= bpsystol>140
            lab values hbp hbp
            lab def hbp 0 "no", modify
            lab def hbp 1 "yes", modify
            
            *START HERE
            clear matrix
            estpost svy: tab hbp if year==2019, percent
            mat b= (nullmat(b) \ e(b)[1, "yes"])
            local year 2019
            estpost svy: tab hbp if year==2020, percent
            mat b= (nullmat(b) \ e(b)[1, "yes"])
            local year `year' 2020
            estpost svy: tab hbp if year==2021, percent
            mat b= (nullmat(b) \ e(b)[1, "yes"])
            local year `year' 2021
            
            mat colname b= Percent
            mat rowname b = `year'
            esttab matrix(b), mlab(none) collab(, lhs("Year"))
            Res.:

            Code:
            . esttab matrix(b), mlab(none) collab(, lhs("Year"))
            
            -------------------------
            Year              Percent
            -------------------------
            2019             18.78204
            2020             18.88521
            2021             19.95637
            -------------------------
            Last edited by Andrew Musau; 12 Nov 2021, 04:14.

            Comment


            • #7
              Andrew Musau That's amazing, thank you so much for taking the time to develop this code!

              Comment

              Working...
              X