Announcement

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

  • How to generate a matrix with weighted counts and proportions (from a complex survey) to use with putexcel?

    Hello everyone
    I am working in some descriptive analysis of a survey. Basically I want to get the counts and proportions of participants each year to put them in a matrix and then export them into excel using putexcel. The problem is I am failing to obtain the weighted counts of the variables to.

    I tried:

    Code:
    svy: tab year, obs percent format(%14.3gc)
    ereturn list
    mat define obs = e(Obs)
    mat list obs
    mat define pct = e(b)'
    mat list pct
    putexcel U7=matrix(obs) V7=matrix(pct), nformat(number_d2) hcenter
    But the return list only stores the unweighted observations and I want the weighted counts.

    I laso try generating the weighted counts and porportions using this command:
    Code:
    svy: tab year, cell count
    However, still can not find a way to the data return by this command with putexcel, let alone store it into a matrix.

    Any help or alternative pathway to accomplish this will be appreciated.
    Thank you


  • #2
    Use proportion in place of tab and then generate the matrix.

    Code:
    webuse nhanes2b, clear
    svyset psuid [pweight=finalwgt], strata(stratid)
    svy: tabulate race, cell count format(%14.8gc)
    *START HERE
    clear matrix
    svy: proportion race
    mat wanted= nullmat(wanted), e(b)'* e(N_pop),e(b)'
    mat colnames wanted= count proportion
    mat l wanted, format(%14.8gc)
    Res.:

    Code:
    . svy: tabulate race, cell count format(%14.8gc)
    (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   |       count   proportion
    ----------+-------------------------
        White | 102,999,549    .87915445
        Black |  11,189,236   .095505919
        Other |   2,968,728    .02533963
              | 
        Total | 117,157,513            1
    ------------------------------------
      Key:  count     =  weighted count
            propor~n  =  cell proportion
    
     
    . mat wanted= nullmat(wanted), e(b)'* e(N_pop),e(b)'
    
    . 
    . mat colnames wanted= count proportion
    
    . 
    . mat l wanted, format(%14.8gc)
    
    wanted[3,2]
                  count   proportion
    1.race  102,999,549    .87915445
    2.race   11,189,236   .095505919
    3.race    2,968,728    .02533963
    
    . 

    Comment

    Working...
    X