Announcement

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

  • Svy: means generating different results and macros

    I'm doing an analysis of the sample adult files from the Behavioral Risk Factor Surveillance Survey (BRFSS) from the Center on Disease Control and Prevention, pooling 3 years of data from 2001, 2007, and 2015; yielding 1,084,878 observations. I'm using complex survey design to complete my analysis in Stata 15.1 SE.

    Code:
    //WEIGHT VARIABLE GENERATION
        {
            generate weightvar = _finalwt
            assert _finalwt == . if _llcpwt ~= .
            replace weightvar = _llcpwt if _llcpwt ~= .
            
            }
            
    //SVYSET FOR ENTIRE MERGED DATASET
        {
            svyset _psu [pw = weightvar], strata(_ststr)
    In my analysis, I'm looking at the means of indicator variables in the dataset for various subpopulations, and using these to do a lincom calculation (for the difference in means between two subpopulations). However, when I input my code for the mean for the indicator variables, I get different estimates depending on whether or not I put all of the indicator variables in the same line of code or not.

    Code:
    foreach i in 2007 2015 {
    
            *rural (mscode == 5)
            svy, subpop(if mscode == 5 & year == `i'): mean bmi //body mass index
            svy, subpop(if mscode == 5 & year == `i'): mean drinkingvol //drinking volume (drinks/per month)
            svy, subpop(if mscode == 5 & year == `i'): mean binarysmoker //smoker or non-smoker
            svy, subpop(if mscode == 5 & year == `i'): mean actlim2 //disability (1= has an activity limitation 0= does not)
            svy, subpop(if mscode == 5 & year == `i'): mean disabilitya //disability (1= has an activity limitation or req. special equip. 0= no)
            
            }
            
    ***********CODE BELOW YIELDS DIFFERENT MEAN ESTIMATES THAN ABOVE
    
    foreach i in 2007 2015 {
    
            svy, subpop(if mscode == 5 & year == `i'): mean bmi drinkingvol ///
            binarysmoker actlim2 disabilitya
    
            }
    I'm attempting to write a more succinct code, so I prefer to have the variables in one line of code, especially as Stata formats the results in one table of easily readable output (which is important to retain for this analysis), but the differences in the estimates are something I'd like to address, whether it's a coding or interpretation issue (as in I haven't coded what I've intended to).

    Along the the lines of writing a more succinct code, I've attempted to use local macros to make the variables bmi drinkingvol binarysmoker actlim2 and disabilitya easily callable in the do-file. However, when I do the code below, Stata gives this error message for lincom: [bmidrinkingvolbinarysmokeractlim2disabilitya] equation not recognized. r(303).

    Code:
    local variables bmi drinkingvol binarysmoker actlim2 disabilitya
    
    foreach i in 2001 2007 2015 {
    
            svy, subpop(if year == `i'): mean `variables', over(primeage2)
            
            lincom [`variables']oldage - [`variables']primeage
            
            }
    This exact Stata error persists even when I define the local macro like so:

    Code:
     local variables "bmi drinkingvol binarysmoker actlim2 disabilitya"
    Using a nested loop yields individual tables for each variable mean, so it is also not optimal:

    Code:
    foreach i in 2001 2007 2015 {
    
        foreach var in bmi drinkingvol binarysmoker actlim2 disabilitya {
        
            svy, subpop(if year == `i'): mean `var', over(primeage2)
            
            }
            
        }
    Here is a dataex, which only has an extract from year 2007:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte mscode float binarysmoker int(drinkingvol bmi) float(actlim2 disabilitya year primeage2) byte gender
    3 0   . 19 1 1 2007 1 1
    3 0   . 36 0 0 2007 1 0
    3 0   . 29 0 0 2007 0 1
    5 0  12 21 0 0 2007 1 1
    2 0   . 31 0 0 2007 0 1
    1 1   . 25 1 1 2007 0 0
    1 1   . 21 . . 2007 1 0
    5 0   1 21 0 0 2007 1 1
    5 1  30 32 0 0 2007 1 0
    5 0   .  . . . 2007 1 1
    5 0   . 30 0 0 2007 0 1
    2 0   . 25 . . 2007 1 0
    1 1   . 26 0 0 2007 0 0
    1 1   . 25 . . 2007 1 0
    1 0   . 24 0 0 2007 0 1
    2 0   . 30 . . 2007 0 1
    1 0   . 38 . . 2007 1 0
    5 0   4 33 0 0 2007 1 1
    1 0   9 22 0 0 2007 . 0
    5 0   . 25 0 0 2007 . 1
    5 0   . 28 0 0 2007 1 1
    5 0   . 23 0 0 2007 0 1
    2 1 105 20 1 1 2007 1 0
    2 1   . 20 0 0 2007 1 1
    1 1   . 32 0 0 2007 1 1
    1 1   . 25 0 0 2007 0 0
    1 0   . 50 1 1 2007 1 1
    3 0  42 22 0 0 2007 0 1
    2 1   . 25 0 0 2007 1 0
    2 0  96 28 . . 2007 1 0
    1 0   . 22 . . 2007 . 1
    1 0   . 28 0 0 2007 0 1
    3 0   . 29 1 1 2007 1 1
    3 0   . 23 1 1 2007 1 1
    3 0   . 31 0 0 2007 1 1
    3 0   . 25 0 0 2007 0 1
    3 1   . 28 1 1 2007 1 0
    3 1   2 55 0 0 2007 . 1
    3 1  60 32 0 0 2007 1 0
    3 0   . 29 0 0 2007 1 1
    5 0   . 23 0 0 2007 0 1
    5 1   . 26 0 0 2007 1 1
    5 0   . 19 0 0 2007 1 1
    3 0   . 25 0 0 2007 0 1
    3 0   . 29 1 1 2007 0 1
    3 0   . 44 0 0 2007 1 1
    3 1   . 27 1 1 2007 0 1
    3 0   9 20 0 0 2007 0 1
    3 0   . 27 1 1 2007 1 0
    3 0   1 27 0 0 2007 1 1
    3 0   8 26 0 0 2007 1 1
    3 0   5 23 0 0 2007 1 1
    3 0   4 26 0 0 2007 1 1
    5 1   . 26 0 0 2007 0 0
    5 0   . 32 1 1 2007 0 1
    3 1   . 29 1 1 2007 0 1
    3 0   . 25 0 0 2007 . 0
    3 0   . 27 0 0 2007 1 1
    3 0   . 22 0 0 2007 0 1
    3 0   . 29 1 1 2007 0 0
    3 0   . 35 0 0 2007 0 1
    3 0   . 29 0 0 2007 1 1
    3 0   . 33 0 0 2007 1 1
    5 0   . 23 0 0 2007 1 1
    5 0   . 28 1 1 2007 0 1
    1 0   . 22 0 0 2007 1 0
    2 0   . 23 1 1 2007 1 1
    3 0   . 24 0 0 2007 0 1
    3 0   .  . 0 0 2007 . 1
    3 1  60 24 1 1 2007 0 0
    3 1   . 25 0 1 2007 0 0
    3 1  36 26 0 0 2007 1 0
    3 1  32 36 0 0 2007 1 1
    3 0  40 25 0 0 2007 0 0
    3 1   . 19 1 1 2007 1 1
    3 0   4 27 1 1 2007 0 1
    3 0 144 24 0 0 2007 1 0
    3 0   . 28 1 1 2007 0 1
    3 1  42 26 0 0 2007 1 1
    3 0   5 24 0 0 2007 1 1
    3 0   . 17 0 0 2007 1 1
    3 0   . 39 0 1 2007 0 1
    3 0   . 24 1 1 2007 0 1
    5 0   8 26 0 1 2007 0 1
    5 0   . 25 0 0 2007 0 0
    1 0   . 23 1 1 2007 0 1
    5 1   6 19 0 0 2007 1 0
    5 0   . 28 0 0 2007 0 0
    5 0   . 28 0 0 2007 0 1
    5 1   . 24 0 0 2007 1 1
    5 0   . 26 0 0 2007 0 1
    5 0   . 32 1 1 2007 1 1
    5 1   . 24 0 0 2007 0 0
    5 0   8 28 0 0 2007 1 0
    5 1   . 22 0 0 2007 0 1
    5 .   . 21 0 0 2007 0 1
    5 0   . 26 0 0 2007 0 1
    5 1 360 23 0 0 2007 1 0
    5 0  12 20 0 0 2007 1 1
    5 1   . 32 0 0 2007 0 1
    5 1   . 22 1 1 2007 1 1
    5 0   . 22 0 0 2007 0 1
    5 0  24  . 0 0 2007 0 1
    5 1   . 23 0 0 2007 1 1
    5 0   8 24 0 0 2007 0 1
    5 0   . 23 0 0 2007 0 1
    5 0   . 29 0 0 2007 0 1
    5 0   6 19 1 1 2007 1 1
    5 0   . 27 0 0 2007 0 1
    5 0  17 32 0 0 2007 0 0
    5 0   . 33 0 0 2007 1 1
    5 0  32 25 0 0 2007 1 0
    5 0   2 21 0 0 2007 1 1
    5 0  40 35 0 0 2007 1 0
    5 1  90 19 0 0 2007 0 1
    5 1   . 20 1 1 2007 0 0
    5 0   3 22 1 1 2007 0 1
    5 0   . 24 0 0 2007 0 0
    5 0   . 23 0 0 2007 1 1
    5 0   4 28 0 0 2007 0 1
    5 0   . 25 1 1 2007 0 1
    5 0   2 18 0 0 2007 0 1
    5 0   6 25 0 0 2007 1 1
    5 0   . 27 1 1 2007 0 0
    5 0   2 23 0 0 2007 1 1
    5 0   .  . 1 1 2007 1 1
    5 0   2 21 0 0 2007 1 1
    5 0   . 24 1 1 2007 0 1
    5 0   0 25 1 1 2007 0 1
    5 0   . 23 1 1 2007 0 1
    5 1   . 21 0 0 2007 0 1
    5 0  21 19 0 0 2007 0 1
    5 0   4 19 0 0 2007 1 1
    5 0   . 25 0 0 2007 1 1
    5 0   . 32 0 0 2007 1 0
    5 0   . 36 0 0 2007 1 0
    5 1  36 28 1 1 2007 1 1
    5 0   . 24 0 0 2007 0 1
    5 0   4 27 0 0 2007 1 0
    5 0   . 28 1 1 2007 0 1
    5 0   . 23 0 0 2007 0 0
    5 0   . 24 0 0 2007 0 0
    5 0   . 21 0 0 2007 0 1
    5 0   5 29 1 1 2007 0 1
    5 1   . 32 0 0 2007 1 1
    5 0   1 20 0 0 2007 1 1
    5 0   0 41 1 1 2007 0 1
    5 0   . 28 0 0 2007 1 1
    5 0  63 27 1 1 2007 0 0
    5 0   1 29 1 1 2007 1 1
    5 0   1 23 0 0 2007 1 1
    5 0   . 38 . . 2007 1 0
    5 0   . 27 1 1 2007 0 0
    5 0   . 26 1 1 2007 1 0
    5 0   8 31 1 1 2007 1 1
    5 0   . 35 0 0 2007 0 0
    5 0  50 24 1 1 2007 0 0
    5 1   4 23 0 0 2007 1 0
    5 1   . 31 0 0 2007 0 1
    5 0   1 25 0 0 2007 0 1
    5 0   . 26 1 1 2007 0 1
    5 0   . 28 1 1 2007 0 1
    5 1  15 27 0 0 2007 1 1
    5 0   . 28 1 1 2007 0 1
    5 0   . 45 1 1 2007 1 1
    5 1  36 18 0 0 2007 0 1
    5 0   . 30 0 0 2007 0 1
    5 0   1 19 0 0 2007 1 1
    5 0   . 27 0 0 2007 0 1
    5 0   . 17 0 0 2007 1 1
    5 0   . 27 0 0 2007 0 1
    5 0   . 31 1 1 2007 0 1
    5 0   .  . 0 0 2007 1 1
    5 1   4 21 0 0 2007 1 1
    5 0   . 22 0 0 2007 0 1
    5 0  16 27 0 0 2007 0 1
    5 0   8 39 1 1 2007 1 0
    5 0   2 29 0 0 2007 0 0
    5 0   . 20 0 0 2007 1 1
    5 0   2 21 0 0 2007 0 1
    5 0   . 16 1 1 2007 0 1
    5 0   8 17 0 0 2007 1 1
    5 0   . 25 1 1 2007 0 1
    5 0   . 16 0 0 2007 0 1
    5 0   . 19 0 0 2007 0 1
    5 1  36 23 0 0 2007 1 0
    5 0   1 22 . . 2007 1 1
    5 0   . 39 0 0 2007 1 1
    5 1 360 23 0 0 2007 1 0
    5 1  12 29 0 0 2007 . 0
    5 0   6 32 0 0 2007 0 1
    5 0  21 22 0 0 2007 1 1
    5 0   . 25 1 1 2007 0 1
    5 1   . 19 1 1 2007 1 0
    5 0   . 27 0 0 2007 0 0
    5 1   . 34 0 0 2007 0 1
    5 0   . 20 0 0 2007 0 1
    5 0  24 27 0 0 2007 1 1
    5 1   . 28 1 1 2007 0 0
    5 0   . 31 0 0 2007 0 1
    5 0   . 23 0 0 2007 0 1
    5 0 126 27 1 1 2007 0 0
    5 0   . 24 1 1 2007 0 0
    5 0   1 26 0 0 2007 0 1
    5 0   . 22 0 0 2007 0 1
    5 0   . 25 0 0 2007 1 1
    5 0  48 19 0 0 2007 0 1
    5 1   . 22 0 0 2007 0 1
    5 0  16 27 0 0 2007 1 1
    5 1   3  . 0 0 2007 . 1
    5 0  30 25 1 1 2007 0 1
    5 0  32 25 0 0 2007 1 1
    5 0   . 35 0 0 2007 0 1
    5 0   4 23 1 1 2007 0 1
    5 0  24 26 0 0 2007 0 1
    5 0   8 23 0 0 2007 1 1
    5 0   . 29 1 1 2007 0 1
    5 0   . 30 0 0 2007 0 1
    5 0   . 31 0 0 2007 0 1
    5 0   .  . 1 1 2007 1 1
    5 1   . 27 0 0 2007 1 0
    5 0   . 24 0 0 2007 0 1
    5 1   8 25 1 1 2007 0 0
    5 1  42 21 0 0 2007 0 0
    5 0   . 28 0 0 2007 1 1
    5 0  60 25 0 0 2007 0 0
    5 0   9 33 0 0 2007 0 0
    5 1   . 23 0 0 2007 1 1
    5 0   . 25 0 0 2007 0 1
    5 1   . 23 1 1 2007 1 1
    5 0   . 22 0 0 2007 1 1
    5 0  15 35 . 9 2007 1 0
    5 0   .  . 1 1 2007 0 1
    5 0   . 28 1 1 2007 0 1
    5 0   . 28 0 0 2007 0 0
    5 0   . 31 1 1 2007 0 1
    5 0   . 24 0 0 2007 0 1
    5 0   . 22 0 0 2007 0 0
    5 0   . 26 0 0 2007 1 1
    5 1  20 33 0 0 2007 1 1
    5 1  50 23 0 0 2007 1 1
    5 1   . 22 0 0 2007 0 1
    5 0  90 26 0 0 2007 1 1
    5 0   . 31 0 0 2007 1 0
    5 0  18 27 0 0 2007 1 1
    5 0   . 26 0 0 2007 0 0
    5 0   . 24 0 0 2007 1 1
    5 0   . 31 1 1 2007 0 0
    5 1  72 25 0 0 2007 1 0
    5 0   . 19 0 0 2007 . 1
    5 0   . 31 0 0 2007 0 0
    5 0   . 24 0 0 2007 0 1
    5 1   3 22 0 0 2007 0 1
    5 1  36 19 0 0 2007 1 1
    5 0 420  . 0 0 2007 0 1
    5 0   . 31 0 0 2007 0 1
    5 1   . 33 0 0 2007 1 0
    5 0   . 29 1 1 2007 0 1
    5 0   3 44 0 0 2007 1 0
    5 0   6 21 0 0 2007 1 1
    5 0   8 25 0 0 2007 1 1
    5 0   . 24 0 0 2007 1 1
    5 0   . 26 0 0 2007 1 1
    5 0   1 25 0 1 2007 1 1
    5 0   3 22 1 1 2007 0 1
    5 0   0 18 0 1 2007 0 1
    5 0  42 24 0 0 2007 0 1
    5 0   . 30 0 0 2007 1 1
    5 0  42 28 0 0 2007 1 0
    5 0   . 39 1 1 2007 1 1
    5 0   6 25 0 0 2007 1 1
    5 0   . 27 0 0 2007 0 0
    5 0   . 20 0 0 2007 . 0
    5 1 105 19 1 1 2007 0 1
    5 0   . 27 0 0 2007 0 1
    5 0   . 23 0 0 2007 0 1
    5 0  12 23 0 0 2007 1 1
    5 0   . 29 0 0 2007 0 1
    5 0   . 31 0 0 2007 1 0
    5 0   4 27 0 0 2007 0 1
    5 0  10 18 0 0 2007 0 1
    5 0   8 27 0 0 2007 0 1
    5 0  12 25 1 1 2007 0 0
    5 0   1 20 0 0 2007 1 1
    5 0  60 22 0 0 2007 0 0
    5 0  21 26 0 0 2007 1 0
    5 1   . 18 0 0 2007 1 1
    5 1  18 30 0 0 2007 1 1
    5 0   . 35 0 0 2007 1 1
    5 0  30  . 0 0 2007 0 1
    5 0  40 30 0 0 2007 0 0
    5 0   4 25 0 0 2007 0 1
    5 0   . 22 0 0 2007 0 1
    5 0   . 25 1 1 2007 0 1
    5 0  10 24 1 1 2007 1 0
    5 0  20 23 0 0 2007 1 0
    5 1   4 24 0 0 2007 1 1
    5 0   2 23 0 0 2007 0 0
    5 0   8 24 0 0 2007 1 1
    5 0   . 23 0 0 2007 1 1
    5 0   . 31 1 1 2007 0 1
    5 1   . 29 1 1 2007 1 1
    5 0   9  . 0 0 2007 0 1
    5 1   1 29 1 1 2007 1 1
    5 0   . 20 0 0 2007 0 1
    5 1  40 22 1 1 2007 0 0
    5 0   . 37 1 1 2007 0 0
    5 0   . 27 0 0 2007 0 1
    5 0  60 25 0 0 2007 0 1
    5 1   . 40 0 0 2007 1 1
    5 0   . 23 0 0 2007 0 1
    5 1   .  . 0 0 2007 0 1
    5 0   . 35 1 1 2007 0 1
    5 1  60 21 0 0 2007 0 1
    5 1  60 22 0 0 2007 0 0
    5 0   . 26 0 0 2007 0 0
    5 0   . 26 0 0 2007 0 1
    5 1  40 23 0 0 2007 0 1
    5 1   . 35 1 1 2007 1 1
    5 0  12 32 0 0 2007 0 0
    5 0   6 33 0 0 2007 0 0
    5 0   . 21 0 0 2007 0 1
    5 0  60 24 0 0 2007 0 1
    5 0   . 31 1 1 2007 0 1
    5 0   . 23 1 1 2007 0 1
    5 0   2 36 1 1 2007 0 1
    5 0  16 26 0 0 2007 0 0
    5 1   . 19 0 0 2007 . 1
    5 0   . 37 0 0 2007 0 1
    5 1   4 20 0 0 2007 0 1
    5 0   . 22 0 0 2007 1 1
    5 0   . 43 1 1 2007 0 1
    5 0   . 19 0 0 2007 1 1
    5 1  16 24 0 0 2007 0 0
    5 0   . 35 1 1 2007 1 0
    5 0   . 29 0 0 2007 . 1
    5 0   . 31 1 1 2007 . 1
    5 0   . 20 1 1 2007 0 1
    5 0   . 31 1 1 2007 1 1
    5 0   . 35 0 0 2007 1 1
    5 0  60 30 0 0 2007 0 0
    5 0   . 30 1 1 2007 0 1
    5 0   . 24 0 0 2007 0 1
    5 0   4 44 0 0 2007 1 0
    5 1   . 27 0 0 2007 0 1
    5 0   4 25 0 . 2007 0 0
    5 0   . 30 . . 2007 0 1
    5 0  30 20 0 0 2007 0 1
    5 1   . 26 0 0 2007 1 1
    5 0   . 30 0 0 2007 1 1
    5 0   . 31 0 0 2007 1 1
    5 0   . 29 0 0 2007 1 0
    5 1   4 30 0 0 2007 0 1
    5 0   . 27 0 0 2007 0 1
    5 0   . 25 0 0 2007 0 1
    5 1   . 25 0 0 2007 0 0
    5 0   . 25 1 1 2007 0 1
    5 0   . 25 0 1 2007 0 1
    5 1   . 25 1 1 2007 0 1
    5 0   . 30 0 0 2007 1 1
    5 0   . 35 1 1 2007 0 0
    5 0   . 41 1 1 2007 1 0
    5 0   . 24 0 0 2007 0 1
    5 1   . 24 1 1 2007 1 0
    5 0   . 29 0 0 2007 1 0
    5 0   . 31 0 0 2007 0 1
    5 0   . 32 . . 2007 0 1
    5 0   . 30 0 0 2007 0 1
    5 0   1 24 0 0 2007 1 1
    5 1   8 19 1 1 2007 0 0
    5 0   . 24 1 1 2007 0 1
    5 0   . 34 0 0 2007 . 1
    5 0   . 32 0 0 2007 0 1
    5 0   . 46 1 1 2007 0 1
    5 1   .  . 0 0 2007 1 1
    5 0   . 32 0 0 2007 1 1
    5 0   . 31 0 0 2007 1 0
    5 1   2 25 0 0 2007 1 0
    5 0   . 28 1 1 2007 0 1
    5 0   . 27 0 0 2007 0 0
    5 0   . 25 0 . 2007 1 1
    2 0   . 26 1 1 2007 1 1
    5 0   . 32 1 1 2007 0 1
    5 0   4 31 0 0 2007 1 1
    5 0   . 19 0 0 2007 1 1
    5 0   8 33 0 0 2007 1 1
    5 0   . 27 0 0 2007 0 1
    5 0   4 26 1 1 2007 0 1
    5 0   . 33 0 0 2007 0 1
    5 0   1 32 1 1 2007 0 0
    5 0   .  . 0 0 2007 0 1
    5 0   . 39 0 0 2007 0 0
    3 0   . 26 1 1 2007 1 1
    3 0   . 15 1 1 2007 1 1
    3 0   .  . 0 0 2007 1 0
    3 0   . 30 0 0 2007 1 0
    3 1  18 31 1 1 2007 0 0
    2 0   . 27 0 0 2007 1 1
    2 0   . 32 1 1 2007 1 0
    3 0   . 33 0 0 2007 0 1
    3 1  45 21 0 0 2007 1 0
    3 0   . 20 0 0 2007 1 1
    3 0   . 27 0 1 2007 0 1
    3 0   . 34 1 1 2007 0 1
    3 0   . 26 0 0 2007 0 0
    3 0   . 25 0 0 2007 0 0
    3 1   . 24 0 0 2007 0 1
    3 0   . 22 0 0 2007 1 1
    3 1   . 38 0 0 2007 0 0
    3 1   . 22 1 1 2007 0 1
    3 1   . 19 1 1 2007 1 0
    1 1   4 26 0 0 2007 0 0
    3 0   . 33 1 1 2007 1 1
    3 0   .  . 0 0 2007 0 1
    3 0   . 21 0 0 2007 1 1
    3 0 180 23 1 1 2007 0 0
    3 0   8 25 1 1 2007 1 0
    3 0   . 37 0 0 2007 0 1
    3 0   3 29 1 1 2007 0 0
    3 0   8 30 0 0 2007 0 1
    3 0   . 27 0 0 2007 1 1
    3 1   . 62 1 1 2007 1 1
    3 0   . 41 0 0 2007 0 1
    3 0   . 21 0 0 2007 0 1
    3 0   4 32 0 0 2007 0 0
    3 0   . 40 1 1 2007 0 1
    3 0   .  . 0 0 2007 1 1
    3 1   . 28 1 1 2007 1 0
    3 1   . 24 0 0 2007 1 1
    3 0   . 24 1 1 2007 0 1
    3 0   . 24 0 0 2007 0 0
    3 0   . 25 0 0 2007 1 1
    3 0   6 20 0 0 2007 1 1
    2 0   . 23 0 0 2007 . 1
    3 0   . 22 0 0 2007 1 0
    3 0   . 44 1 1 2007 0 1
    3 0   . 39 1 1 2007 1 1
    3 1   . 22 0 0 2007 0 1
    3 1 120 30 0 0 2007 1 1
    3 0   . 21 1 1 2007 0 1
    3 0   . 29 0 0 2007 1 1
    3 0   . 22 0 0 2007 0 0
    3 0   1 26 0 0 2007 1 1
    3 0   . 34 0 0 2007 1 1
    3 0   2 27 0 0 2007 0 1
    3 0   5 17 0 0 2007 1 1
    3 1   . 27 0 0 2007 1 1
    3 0   . 20 0 0 2007 1 1
    3 1   . 29 0 0 2007 0 0
    3 0   .  . 0 0 2007 1 1
    3 0  32 28 0 0 2007 1 1
    3 0   4 22 0 0 2007 0 1
    3 0   . 23 0 0 2007 1 1
    3 1   . 26 1 1 2007 1 1
    3 0   0 21 0 0 2007 1 1
    3 0   . 23 0 0 2007 1 1
    3 0   . 19 1 1 2007 0 1
    3 0   . 25 0 0 2007 0 0
    3 0   . 39 1 1 2007 1 1
    3 1 160 29 0 0 2007 1 0
    1 0   . 36 0 0 2007 1 0
    3 0   . 23 1 1 2007 0 1
    3 0   . 23 0 0 2007 0 1
    1 0   9 25 0 0 2007 1 0
    1 0   . 26 0 0 2007 0 0
    2 0   . 33 1 1 2007 0 1
    2 0   . 24 0 0 2007 0 0
    2 0   4 27 0 0 2007 0 0
    2 0   . 43 0 0 2007 0 1
    2 0   . 18 1 1 2007 1 1
    2 0   4 36 1 1 2007 0 0
    2 0   . 32 1 1 2007 0 1
    3 0   . 35 1 1 2007 1 0
    3 1   .  . 1 1 2007 1 1
    3 1   . 37 0 0 2007 0 1
    3 0   2 36 0 0 2007 1 0
    3 0   . 22 0 0 2007 1 1
    3 1   . 34 0 0 2007 1 1
    3 1   . 38 1 1 2007 0 1
    2 1   . 27 0 0 2007 1 0
    2 0   . 28 0 0 2007 0 1
    2 1   . 23 0 0 2007 1 1
    2 1   4 33 1 1 2007 0 0
    3 1  18 18 0 0 2007 . 1
    3 0   . 23 0 0 2007 1 0
    3 1   . 40 1 1 2007 1 1
    3 0   . 30 1 1 2007 0 1
    3 0  10 27 0 0 2007 1 0
    3 0   4 23 0 0 2007 1 0
    3 0   . 28 0 0 2007 1 1
    3 1  50 28 1 1 2007 0 0
    2 1   . 24 1 1 2007 1 1
    2 0   . 38 0 0 2007 1 0
    2 0   . 30 1 1 2007 0 1
    2 0   4 22 1 1 2007 0 1
    3 0   . 31 0 0 2007 1 0
    3 0   1 26 0 0 2007 . 0
    3 0   . 22 0 0 2007 0 1
    3 0  24 38 0 0 2007 1 0
    3 1  25 22 1 1 2007 0 0
    end
    label values mscode metroarea
    label def metroarea 1 "center city of MSA", modify
    label def metroarea 2 "outside center city but inside county w/ center city", modify
    label def metroarea 3 "inside suburban county of MSA", modify
    label def metroarea 5 "not in MSA", modify
    label values actlim2 yesnobinary
    label values disabilitya yesnobinary
    label def yesnobinary 0 "no", modify
    label def yesnobinary 1 "yes", modify
    label values primeage2 page2
    label def page2 0 "oldage", modify
    label def page2 1 "primeage", modify
    label values gender gender
    label def gender 0 "male", modify
    label def gender 1 "female", modify
    Any assistance in helping me simplify my code and figure out why I'm generating different point estimates would be helpful! Thank you in advance!
    Last edited by Kassandra Martinchek; 17 Jul 2018, 09:47.

  • #2
    However, when I input my code for the mean for the indicator variables, I get different estimates depending on whether or not I put all of the indicator variables in the same line of code or not.
    -means- is an estimation command in Stata. Consequently, just as a regression will omit any observation where any of the specified variables contains a missing values, -means- does that too. So your command specifying several variables at once calculates the means restricted to the subset of observations where all of those variables are non-missing. Of course, when you run the variables through -means- one at a time, you only lose those observations where that particular variable is missing.


    Comment


    • #3
      Note: Crossed with Clyde's post above.


      Thanks for the dataex. . Unfortunately, it omits the stratum and weight statements, so we cannot run your code as is. However it is easy to reproduce the problem with (non-svy) mean.

      Code:
        mean bmi
        mean drinkingvol
        mean bmi drinkingvol
      The reason for the differing results with the third statement; is clear: here are 481 non-missing observations for mean bmi and 179 for mean drinkingvol. For the third mean command, only 175 observations have non-missing values for both variables. Thus the estimation sample differs, so the results differ.
      For future reference: The problem would have been obvious if, as FAQ 12 asks, you had shown us not only the code you typed, but what Stata had responded.
      .
      Code:
      .        mean bmi //body mass index
      
      Mean estimation                   Number of obs   =        481
      
      ------------------------------------------------------------
                   |       Mean   Std. Err.     [95% Conf. Interval]
      -------------+------------------------------------------------
               bmi |   26.94802   .2759073      26.40589    27.49016
      --------------------------------------------------------------
      .         mean drinkingvol //drinking volume (drinks/per month)
      
      Mean estimation                   Number of obs   =        179
      
      --------------------------------------------------------------
                   |       Mean   Std. Err.     [95% Conf. Interval]
      -------------+------------------------------------------------
       drinkingvol |   28.06704    4.14368      19.88998     36.2441
      --------------------------------------------------------------
      
      .         mean bmi drinkingvol
      
      Mean estimation                   Number of obs   =        174
      
      --------------------------------------------------------------
                   |       Mean   Std. Err.     [95% Conf. Interval]
      -------------+------------------------------------------------
               bmi |   25.77586   .4219045      24.94312    26.60861
       drinkingvol |   26.08046   3.607563      18.95995    33.20096
      --------------------------------------------------------------
      Steve Samuels
      Statistical Consulting
      [email protected]

      Stata 14.2

      Comment


      • #4
        Steve & Clyde,

        Thank you for your quick responses. Apologies again I did not include the full output from Stata.

        I'm wondering if there's a way to append the mean output together so that the output generated from the command run from a set of one-variable -svy: mean- will look like the table generated from the multiple-variable -svy:mean- command.

        I was attempting to adapt the code from this stack exchange to create a matrix: https://stackoverflow.com/questions/...les-in-surveys, but at
        Code:
        `gpname' n mean se sd using `d_results', replace
        I get an error:
        (note: file results.dta not found)
        file results.dta could not be opened
        r(603);
        So, I'm trying to access the matrix and generate streamlined output between all of the variables, but haven't been successful.

        The "best" output in terms of results being shown ideally is generated with:

        Code:
        foreach i in 2001 2007 2015 {
        
                svy, subpop(if year == `i'): mean bmi drinkingvol binarysmoker ///
                actlim2 disabilitya, over(primeage2)
              
                }
        [CODE]

        Code:
        Survey: Mean estimation
        
        Number of strata =   1,006      Number of obs   =      402,538
        Number of PSUs   = 402,538      Population size =  314,573,866
                                        Subpop. no. obs =      185,027
                                        Subpop. size    = 98,900,405.4
                                        Design df       =      401,532
        
               oldage: primeage2 = oldage
             primeage: primeage2 = primeage
        
        --------------------------------------------------------------
                     |             Linearized
                Over |       Mean   Std. Err.     [95% Conf. Interval]
        -------------+------------------------------------------------
        bmi          |
              oldage |   26.64923   .0339775      26.58264    26.71583
            primeage |   26.74561    .032551      26.68181    26.80941
        -------------+------------------------------------------------
        drinkingvol  |
              oldage |   25.05506   .5778401      23.92251    26.18761
            primeage |   22.80918   .3884865      22.04776     23.5706
        -------------+------------------------------------------------
        binarysmoker |
              oldage |   .1334177   .0023494       .128813    .1380224
            primeage |   .2394942   .0027243      .2341546    .2448337
        -------------+------------------------------------------------
        actlim2      |
              oldage |   .2432321   .0029152      .2375184    .2489458
            primeage |   .1237564   .0019972       .119842    .1276708
        -------------+------------------------------------------------
        disabilitya  |
              oldage |   .2685104   .0031874      .2622632    .2747576
            primeage |   .1347127   .0023819      .1300442    .1393812
        --------------------------------------------------------------
        Note: 1295 strata omitted because they contain no
              subpopulation members.

        Versus the code that makes more "sense" to implement as to not drop observations:

        Code:
        foreach i in 2001 2007 2015 {
        
                svy, subpop(if year == `i'): mean bmi, over(primeage2)
                
                }
        Code:
        Survey: Mean estimation
        
        Number of strata =     333      Number of obs   =      414,513
        Number of PSUs   = 414,513      Population size =  300,065,242
                                        Subpop. no. obs =      182,862
                                        Subpop. size    =  177,701,615
                                        Design df       =      414,180
        
               oldage: primeage2 = oldage
             primeage: primeage2 = primeage
        
        --------------------------------------------------------------
                     |             Linearized
                Over |       Mean   Std. Err.     [95% Conf. Interval]
        -------------+------------------------------------------------
        bmi          |
              oldage |    26.5266          .             .           .
            primeage |   26.45845          .             .           .
        --------------------------------------------------------------
        
        )
        With one of the above tables for every indicator variable.

        I've tried an updated dataex with PSU, strata, and weight:

        Code:
        * Example generated by -dataex-. 
        clear
        input byte mscode float binarysmoker int(drinkingvol bmi) float(actlim2 disabilitya year primeage2) byte gender long _psu double _finalwt long _ststr
        3 0   . 19 1 1 2007 1 1 2007020938   968.190340490445 1081
        3 0   . 36 0 0 2007 1 0 2007018676 473.38169892728035 1051
        3 0   . 29 0 0 2007 0 1 2007033229   552.628855844674 1052
        5 0  12 21 0 0 2007 1 1 2007022510  999.9470231181489 1091
        2 0   . 31 0 0 2007 0 1 2007034169 293.46647223742053 1061
        1 1   . 25 1 1 2007 0 0 2007034271  436.0075624016442 1061
        1 1   . 21 . . 2007 1 0 2007034802 1304.3699697416055 1061
        5 0   1 21 0 0 2007 1 1 2007022279   494.880708629854 1091
        5 1  30 32 0 0 2007 1 0 2007022854  861.4491305876963 1101
        5 0   .  . . . 2007 1 1 2007041808 343.94468613465415 1142
        5 0   . 30 0 0 2007 0 1 2007016973 259.72116429641864 1032
        2 0   . 25 . . 2007 1 0 2007037925   970.686299762309 1102
        1 1   . 26 0 0 2007 0 0 2007022993  321.7784145502083 1101
        1 1   . 25 . . 2007 1 0 2007023306  274.4606860855031 1101
        1 0   . 24 0 0 2007 0 1 2007038841   361.135071365526 1101
        2 0   . 30 . . 2007 0 1 2007017469  440.8999902345458 1042
        1 0   . 38 . . 2007 1 0 2007040624 1363.7599916495299 1131
        5 0   4 33 0 0 2007 1 1 2007004533 167.74823891924282 1061
        1 0   9 22 0 0 2007 . 0 2007036019   1708.98427931414 1081
        5 0   . 25 0 0 2007 . 1 2007035248  71.38700421888036 1071
        5 0   . 28 0 0 2007 1 1 2007051028  67.46880209274296 1072
        5 0   . 23 0 0 2007 0 1 2007046700 466.59838292468504 1021
        2 1 105 20 1 1 2007 1 0 2007023919 1149.6428128270911 1112
        2 1   . 20 0 0 2007 1 1 2007039139  697.3315203214092 1111
        1 1   . 32 0 0 2007 1 1 2007059176 387.09350728883163 1162
        1 1   . 25 0 0 2007 0 0 2007027890  296.3749314232059 1151
        1 0   . 50 1 1 2007 1 1 2007035956  729.0584130271814 1081
        3 0  42 22 0 0 2007 0 1 2007035927  701.2305653945078 1082
        2 1   . 25 0 0 2007 1 0 2007002384  1252.510420644777 1031
        2 0  96 28 . . 2007 1 0 2007002436   674.027922989661 1031
        1 0   . 22 . . 2007 . 1 2007024784 1195.6054422405361 1122
        1 0   . 28 0 0 2007 0 1 2007040067 143.76945238950614 1121
        3 0   . 29 1 1 2007 1 1 2007029411  408.7145818795174 1011
        3 0   . 23 1 1 2007 1 1 2007005974  468.7338333346058 1081
        3 0   . 31 0 0 2007 1 1 2007005994   968.190340490445 1081
        3 0   . 25 0 0 2007 0 1 2007006094 296.33997722315706 1081
        3 1   . 28 1 1 2007 1 0 2007006171  762.6358429871723 1081
        3 1   2 55 0 0 2007 . 1 2007006231  2588.403964942214 1081
        3 1  60 32 0 0 2007 1 0 2007006324  762.6358429871723 1081
        3 0   . 29 0 0 2007 1 1 2007006361  351.5503750009544 1081
        5 0   . 23 0 0 2007 0 1 2007011303 106.10443176489319 1141
        5 1   . 26 0 0 2007 1 1 2007011567  60.48975922433146 1141
        5 0   . 19 0 0 2007 1 1 2007019964 122.91108832155695 1071
        3 0   . 25 0 0 2007 0 1 2007020652  959.8916303776625 1082
        3 0   . 29 1 1 2007 0 1 2007020658  318.8620666072995 1081
        3 0   . 44 0 0 2007 1 1 2007020696   968.190340490445 1081
        3 1   . 27 1 1 2007 0 1 2007020761  318.8620666072995 1081
        3 0   9 20 0 0 2007 0 1 2007020836  592.6799544463141 1081
        3 0   . 27 1 1 2007 1 0 2007020861 2295.8132665274575 1082
        3 0   1 27 0 0 2007 1 1 2007020909  801.8897255111872 1082
        3 0   8 26 0 0 2007 1 1 2007021076  2573.414390897623 1081
        3 0   5 23 0 0 2007 1 1 2007021145  351.5503750009544 1081
        3 0   4 26 0 0 2007 1 1 2007021266  1065.504508949857 1081
        5 1   . 26 0 0 2007 0 0 2007035210 120.28146290932942 1071
        5 0   . 32 1 1 2007 0 1 2007035758 37.620153383510335 1071
        3 1   . 29 1 1 2007 0 1 2007035940  592.6799544463141 1081
        3 0   . 25 0 0 2007 . 0 2007035945  473.7381798467396 1081
        3 0   . 27 0 0 2007 1 1 2007035946   968.190340490445 1081
        3 0   . 22 0 0 2007 0 1 2007036074 296.33997722315706 1081
        3 0   . 29 1 1 2007 0 0 2007036244  415.1057278704677 1081
        3 0   . 35 0 0 2007 0 1 2007036284 308.32535336995824 1081
        3 0   . 29 0 0 2007 1 1 2007036389   968.190340490445 1081
        3 0   . 33 0 0 2007 1 1 2007036441   968.190340490445 1081
        5 0   . 23 0 0 2007 1 1 2007041758  83.29617139425825 1141
        5 0   . 28 1 1 2007 0 1 2007041790  54.86522275732439 1141
        1 0   . 22 0 0 2007 1 0 2007043190  645.4703164869175 1152
        2 0   . 23 1 1 2007 1 1 2007049143 218.21360129709174 1051
        3 0   . 24 0 0 2007 0 1 2007051063 296.33997722315706 1081
        3 0   .  . 0 0 2007 . 1 2007051117 2468.3187059702914 1081
        3 1  60 24 1 1 2007 0 0 2007051160  947.4763596934793 1081
        3 1   . 25 0 1 2007 0 0 2007051177  633.1452989635005 1082
        3 1  36 26 0 0 2007 1 0 2007051190   1813.77092410031 1081
        3 1  32 36 0 0 2007 1 1 2007051214 1286.7071954488115 1081
        3 0  40 25 0 0 2007 0 0 2007051278  947.4763596934793 1081
        3 1   . 19 1 1 2007 1 1 2007051320  703.1007500019088 1081
        3 0   4 27 1 1 2007 0 1 2007051371   637.724133214599 1081
        3 0 144 24 0 0 2007 1 0 2007051475  453.4427310250775 1081
        3 0   . 28 1 1 2007 0 1 2007051504 465.87723066597766 1081
        3 1  42 26 0 0 2007 1 1 2007051521  703.1007500019088 1081
        3 0   5 24 0 0 2007 1 1 2007051568  527.3255625014315 1081
        3 0   . 17 0 0 2007 1 1 2007051588    968.36500559593 1082
        3 0   . 39 0 1 2007 0 1 2007051601  924.9760601098747 1081
        3 0   . 24 1 1 2007 0 1 2007051665 465.87723066597766 1081
        5 0   8 26 0 1 2007 0 1 2007051908  233.4637075509151 1092
        5 0   . 25 0 0 2007 0 0 2007052633 484.15020111674903 1091
        1 0   . 23 1 1 2007 0 1 2007053476   180.567535682763 1101
        5 1   6 19 0 0 2007 1 0 2007056956  573.6500035250397 1141
        5 0   . 28 0 0 2007 0 0 2007056976 142.85091023701673 1141
        5 0   . 28 0 0 2007 0 1 2007057210 114.15613083889708 1141
        5 1   . 24 0 0 2007 1 1 2007002550 195.98526541760506 1032
        5 0   . 26 0 0 2007 0 1 2007006421  218.6065596297231 1091
        5 0   . 32 1 1 2007 1 1 2007006422  603.7698662494778 1091
        5 1   . 24 0 0 2007 0 0 2007006430 222.29520320943598 1091
        5 0   8 28 0 0 2007 1 0 2007006451 425.54367929420727 1091
        5 1   . 22 0 0 2007 0 1 2007006462 448.82524403734595 1092
        5 .   . 21 0 0 2007 0 1 2007006510  278.1070146265717 1091
        5 0   . 26 0 0 2007 0 1 2007006620   299.243383402965 1091
        5 1 360 23 0 0 2007 1 0 2007006637 425.54367929420727 1091
        5 0  12 20 0 0 2007 1 1 2007006685 454.31016042313547 1091
        5 1   . 32 0 0 2007 0 1 2007006720  149.6216917014825 1091
        5 1   . 22 1 1 2007 1 1 2007006734 164.96023620995132 1091
        5 0   . 22 0 0 2007 0 1 2007006742  149.6216917014825 1091
        5 0  24  . 0 0 2007 0 1 2007006751   299.243383402965 1091
        5 1   . 23 0 0 2007 1 1 2007006764 227.15508021156774 1091
        5 0   8 24 0 0 2007 0 1 2007006766  278.1070146265717 1091
        5 0   . 23 0 0 2007 0 1 2007006782  278.1070146265717 1091
        5 0   . 29 0 0 2007 0 1 2007006785  437.2131192594462 1091
        5 0   6 19 1 1 2007 1 1 2007006831 454.31016042313547 1091
        5 0   . 27 0 0 2007 0 1 2007006909  278.1070146265717 1091
        5 0  17 32 0 0 2007 0 0 2007006911 484.15020111674903 1091
        5 0   . 33 0 0 2007 1 1 2007006920 329.92047241990264 1091
        5 0  32 25 0 0 2007 1 0 2007006930  851.0873585884145 1091
        5 0   2 21 0 0 2007 1 1 2007006934 454.31016042313547 1091
        5 0  40 35 0 0 2007 1 0 2007006950  851.0873585884145 1091
        5 1  90 19 0 0 2007 0 1 2007006963  149.6216917014825 1091
        5 1   . 20 1 1 2007 0 0 2007006971 222.29520320943598 1091
        5 0   3 22 1 1 2007 0 1 2007006992  437.2131192594462 1091
        5 0   . 24 0 0 2007 0 0 2007006994  389.5654437656637 1091
        5 0   . 23 0 0 2007 1 1 2007006997  905.5744340281796 1092
        5 0   4 28 0 0 2007 0 1 2007007000 448.82524403734595 1092
        5 0   . 25 1 1 2007 0 1 2007007023  437.2131192594462 1091
        5 0   2 18 0 0 2007 0 1 2007007049  417.1605219398576 1091
        5 0   6 25 0 0 2007 1 1 2007007068  681.4047692609373 1092
        5 0   . 27 1 1 2007 0 0 2007007081 242.07510055837452 1091
        5 0   2 23 0 0 2007 1 1 2007007112 164.96023620995132 1091
        5 0   .  . 1 1 2007 1 1 2007007128  494.8367942604678 1092
        5 0   2 21 0 0 2007 1 1 2007007132   494.880708629854 1091
        5 0   . 24 1 1 2007 0 1 2007007153  655.0518530210433 1091
        5 0   0 25 1 1 2007 0 1 2007007173 139.05350731328585 1091
        5 0   . 23 1 1 2007 0 1 2007007190  218.6065596297231 1091
        5 1   . 21 0 0 2007 0 1 2007007218   299.243383402965 1091
        5 0  21 19 0 0 2007 0 1 2007007234  218.6065596297231 1091
        5 0   4 19 0 0 2007 1 1 2007007239 329.92047241990264 1091
        5 0   . 25 0 0 2007 1 1 2007007260 329.92047241990264 1091
        5 0   . 32 0 0 2007 1 0 2007007308 1276.6310378826217 1091
        5 0   . 36 0 0 2007 1 0 2007007320  851.0873585884145 1091
        5 1  36 28 1 1 2007 1 1 2007007334  905.5744340281796 1092
        5 0   . 24 0 0 2007 0 1 2007007343  278.1070146265717 1091
        5 0   4 27 0 0 2007 1 0 2007007347 2372.0853890156523 1092
        5 0   . 28 1 1 2007 0 1 2007007367  218.6065596297231 1091
        5 0   . 23 0 0 2007 0 0 2007007372 484.15020111674903 1091
        5 0   . 24 0 0 2007 0 0 2007007373  389.5654437656637 1091
        5 0   . 21 0 0 2007 0 1 2007007395  218.6065596297231 1091
        5 0   5 29 1 1 2007 0 1 2007007400   299.243383402965 1091
        5 1   . 32 0 0 2007 1 1 2007007437  603.7698662494778 1091
        5 0   1 20 0 0 2007 1 1 2007007472 454.31016042313547 1091
        5 0   0 41 1 1 2007 0 1 2007007475  218.6065596297231 1091
        5 0   . 28 0 0 2007 1 1 2007007489 109.97349080663422 1091
        5 0  63 27 1 1 2007 0 0 2007007495  584.3481656484955 1091
        5 0   1 29 1 1 2007 1 1 2007007515 454.31016042313547 1091
        5 0   1 23 0 0 2007 1 1 2007007522  603.7698662494778 1091
        5 0   . 38 . . 2007 1 0 2007007523  996.8387635999524 1091
        5 0   . 27 1 1 2007 0 0 2007007550 242.07510055837452 1091
        5 0   . 26 1 1 2007 1 0 2007013779   638.258876556601 1092
        5 0   8 31 1 1 2007 1 1 2007013788  659.8409448398053 1091
        5 0   . 35 0 0 2007 0 0 2007013822  389.5654437656637 1091
        5 0  50 24 1 1 2007 0 0 2007020227 105.39476515583925 1071
        5 1   4 23 0 0 2007 1 0 2007021307   638.258876556601 1092
        5 1   . 31 0 0 2007 0 1 2007021340   299.243383402965 1091
        5 0   1 25 0 0 2007 0 1 2007021374  149.6216917014825 1091
        5 0   . 26 1 1 2007 0 1 2007021430 139.05350731328585 1091
        5 0   . 28 1 1 2007 0 1 2007021457  218.6065596297231 1091
        5 1  15 27 0 0 2007 1 1 2007021461 454.31016042313547 1091
        5 0   . 28 1 1 2007 0 1 2007021482  218.6065596297231 1091
        5 0   . 45 1 1 2007 1 1 2007021490 227.15508021156774 1091
        5 1  36 18 0 0 2007 0 1 2007021496 139.05350731328585 1091
        5 0   . 30 0 0 2007 0 1 2007021511   69.5205840414426 1092
        5 0   1 19 0 0 2007 1 1 2007021514 164.96023620995132 1091
        5 0   . 27 0 0 2007 0 1 2007021552 327.88074161643414 1092
        5 0   . 17 0 0 2007 1 1 2007021567   494.880708629854 1091
        5 0   . 27 0 0 2007 0 1 2007021574 109.30327981486155 1091
        5 0   . 31 1 1 2007 0 1 2007021587 224.41262201867298 1092
        5 0   .  . 0 0 2007 1 1 2007021677 329.92047241990264 1091
        5 1   4 21 0 0 2007 1 1 2007021679 329.92047241990264 1091
        5 0   . 22 0 0 2007 0 1 2007021711  437.2131192594462 1091
        5 0  16 27 0 0 2007 0 1 2007021750 139.05350731328585 1091
        5 0   8 39 1 1 2007 1 0 2007021752 425.54367929420727 1091
        5 0   2 29 0 0 2007 0 0 2007021783 484.15020111674903 1091
        5 0   . 20 0 0 2007 1 1 2007021832   612.638825405807 1091
        5 0   2 21 0 0 2007 0 1 2007021868 139.05350731328585 1091
        5 0   . 16 1 1 2007 0 1 2007021875   299.243383402965 1091
        5 0   8 17 0 0 2007 1 1 2007021895 164.96023620995132 1091
        5 0   . 25 1 1 2007 0 1 2007021958   299.243383402965 1091
        5 0   . 16 0 0 2007 0 1 2007021974  218.6065596297231 1091
        5 0   . 19 0 0 2007 0 1 2007021983   299.243383402965 1091
        5 1  36 23 0 0 2007 1 0 2007021988 1994.8928595517145 1092
        5 0   1 22 . . 2007 1 1 2007022023 164.96023620995132 1091
        5 0   . 39 0 0 2007 1 1 2007022096   494.880708629854 1091
        5 1 360 23 0 0 2007 1 0 2007022155  851.0873585884145 1091
        5 1  12 29 0 0 2007 . 0 2007022163  400.9232447436346 1092
        5 0   6 32 0 0 2007 0 1 2007022165 289.35496439898236 1091
        5 0  21 22 0 0 2007 1 1 2007022216  301.8849331247389 1091
        5 0   . 25 1 1 2007 0 1 2007022218  278.1070146265717 1091
        5 1   . 19 1 1 2007 1 0 2007022246 1073.5695173216452 1091
        5 0   . 27 0 0 2007 0 0 2007022282 222.29520320943598 1091
        5 1   . 34 0 0 2007 0 1 2007022284  278.1070146265717 1091
        5 0   . 20 0 0 2007 0 1 2007022290  218.6065596297231 1091
        5 0  24 27 0 0 2007 1 1 2007022301 227.15508021156774 1091
        5 1   . 28 1 1 2007 0 0 2007022327 222.29520320943598 1091
        5 0   . 31 0 0 2007 0 1 2007022340 434.03244659847354 1091
        5 0   . 23 0 0 2007 0 1 2007022407  149.6216917014825 1091
        5 0 126 27 1 1 2007 0 0 2007022460 292.14815609003796 1092
        5 0   . 24 1 1 2007 0 0 2007022463 444.59040641887196 1091
        5 0   1 26 0 0 2007 0 1 2007022468   299.243383402965 1091
        5 0   . 22 0 0 2007 0 1 2007022474  437.2131192594462 1091
        5 0   . 25 0 0 2007 1 1 2007022525 1022.1071538914059 1092
        5 0  48 19 0 0 2007 0 1 2007022537  218.6065596297231 1091
        5 1   . 22 0 0 2007 0 1 2007022544 139.05350731328585 1091
        5 0  16 27 0 0 2007 1 1 2007022547 329.92047241990264 1091
        5 1   3  . 0 0 2007 . 1 2007022550   299.243383402965 1091
        5 0  30 25 1 1 2007 0 1 2007022611  218.6065596297231 1091
        5 0  32 25 0 0 2007 1 1 2007036504  681.4047692609373 1092
        5 0   . 35 0 0 2007 0 1 2007036508 139.05350731328585 1091
        5 0   4 23 1 1 2007 0 1 2007036536 327.88074161643414 1092
        5 0  24 26 0 0 2007 0 1 2007036545  149.6216917014825 1091
        5 0   8 23 0 0 2007 1 1 2007036675  603.7698662494778 1091
        5 0   . 29 1 1 2007 0 1 2007036724  437.2131192594462 1091
        5 0   . 30 0 0 2007 0 1 2007036750 311.31256843263947 1091
        5 0   . 31 0 0 2007 0 1 2007036757 139.05350731328585 1091
        5 0   .  . 1 1 2007 1 1 2007036762 256.55279697114185 1092
        5 1   . 27 0 0 2007 1 0 2007036771  665.0232988777689 1091
        5 0   . 24 0 0 2007 0 1 2007036885 139.05350731328585 1091
        5 1   8 25 1 1 2007 0 0 2007036903 222.29520320943598 1091
        5 1  42 21 0 0 2007 0 0 2007036912   666.885609628308 1091
        5 0   . 28 0 0 2007 1 1 2007036934  905.5744340281796 1092
        5 0  60 25 0 0 2007 0 0 2007036975 242.07510055837452 1091
        5 0   9 33 0 0 2007 0 0 2007037001 194.78272188283185 1091
        5 1   . 23 0 0 2007 1 1 2007037008 329.92047241990264 1091
        5 0   . 25 0 0 2007 0 1 2007037024   299.243383402965 1091
        5 1   . 23 1 1 2007 1 1 2007037025   612.638825405807 1091
        5 0   . 22 0 0 2007 1 1 2007037041  905.5744340281796 1092
        5 0  15 35 . 9 2007 1 0 2007037066  715.7130115477634 1091
        5 0   .  . 1 1 2007 0 1 2007037080   299.243383402965 1091
        5 0   . 28 1 1 2007 0 1 2007037088  149.6216917014825 1091
        5 0   . 28 0 0 2007 0 0 2007037123  389.5654437656637 1091
        5 0   . 31 1 1 2007 0 1 2007037133   299.243383402965 1091
        5 0   . 24 0 0 2007 0 1 2007037149  218.6065596297231 1091
        5 0   . 22 0 0 2007 0 0 2007037197 444.59040641887196 1091
        5 0   . 26 0 0 2007 1 1 2007037230 227.15508021156774 1091
        5 1  20 33 0 0 2007 1 1 2007037232   494.880708629854 1091
        5 1  50 23 0 0 2007 1 1 2007037250   494.880708629854 1091
        5 1   . 22 0 0 2007 0 1 2007037291  74.81084585074125 1091
        5 0  90 26 0 0 2007 1 1 2007037307 329.92047241990264 1091
        5 0   . 31 0 0 2007 1 0 2007037326 1330.0465977555377 1091
        5 0  18 27 0 0 2007 1 1 2007037339 164.96023620995132 1091
        5 0   . 26 0 0 2007 0 0 2007037356   666.885609628308 1091
        5 0   . 24 0 0 2007 1 1 2007037379 164.96023620995132 1091
        5 0   . 31 1 1 2007 0 0 2007037389  389.5654437656637 1091
        5 1  72 25 0 0 2007 1 0 2007037420  715.7130115477634 1091
        5 0   . 19 0 0 2007 . 1 2007037459  149.6216917014825 1091
        5 0   . 31 0 0 2007 0 0 2007037472  666.8264320113815 1092
        5 0   . 24 0 0 2007 0 1 2007037537  278.1070146265717 1091
        5 1   3 22 0 0 2007 0 1 2007037546  149.6216917014825 1091
        5 1  36 19 0 0 2007 1 1 2007037576 329.92047241990264 1091
        5 0 420  . 0 0 2007 0 1 2007037579   299.243383402965 1091
        5 0   . 31 0 0 2007 0 1 2007037622   299.243383402965 1091
        5 1   . 33 0 0 2007 1 0 2007037638 4744.1707780313045 1092
        5 0   . 29 1 1 2007 0 1 2007037696  218.6065596297231 1091
        5 0   3 44 0 0 2007 1 0 2007037747  1276.517753113202 1092
        5 0   6 21 0 0 2007 1 1 2007037770  603.7698662494778 1091
        5 0   8 25 0 0 2007 1 1 2007037802 454.31016042313547 1091
        5 0   . 24 0 0 2007 1 1 2007051714  603.7698662494778 1091
        5 0   . 26 0 0 2007 1 1 2007051717 454.31016042313547 1091
        5 0   1 25 0 1 2007 1 1 2007051725 454.31016042313547 1091
        5 0   3 22 1 1 2007 0 1 2007051764 327.88074161643414 1092
        5 0   0 18 0 1 2007 0 1 2007051786  218.6065596297231 1091
        5 0  42 24 0 0 2007 0 1 2007051793  149.6216917014825 1091
        5 0   . 30 0 0 2007 1 1 2007051834  342.1007530527868 1091
        5 0  42 28 0 0 2007 1 0 2007051856 1073.4742516797978 1092
        5 0   . 39 1 1 2007 1 1 2007051874   494.880708629854 1091
        5 0   6 25 0 0 2007 1 1 2007051887  301.8849331247389 1091
        5 0   . 27 0 0 2007 0 0 2007051895 484.15020111674903 1091
        5 0   . 20 0 0 2007 . 0 2007051924 3207.6705979220037 1091
        5 1 105 19 1 1 2007 0 1 2007051930  149.6216917014825 1091
        5 0   . 27 0 0 2007 0 1 2007051938  218.6065596297231 1091
        5 0   . 23 0 0 2007 0 1 2007051942 139.05350731328585 1091
        5 0  12 23 0 0 2007 1 1 2007051984 329.92047241990264 1091
        5 0   . 29 0 0 2007 0 1 2007051989   299.243383402965 1091
        5 0   . 31 0 0 2007 1 0 2007051991  1581.530600071859 1091
        5 0   4 27 0 0 2007 0 1 2007052017 448.82524403734595 1092
        5 0  10 18 0 0 2007 0 1 2007052026  149.6216917014825 1091
        5 0   8 27 0 0 2007 0 1 2007052044 139.05350731328585 1091
        5 0  12 25 1 1 2007 0 0 2007052081 444.59040641887196 1091
        5 0   1 20 0 0 2007 1 1 2007052111 340.70238463046866 1092
        5 0  60 22 0 0 2007 0 0 2007052123 484.15020111674903 1091
        5 0  21 26 0 0 2007 1 0 2007052170  1276.517753113202 1092
        5 1   . 18 0 0 2007 1 1 2007052194 164.96023620995132 1091
        5 1  18 30 0 0 2007 1 1 2007052195   494.880708629854 1091
        5 0   . 35 0 0 2007 1 1 2007052201  171.0503765263934 1091
        5 0  30  . 0 0 2007 0 1 2007052223  218.6065596297231 1091
        5 0  40 30 0 0 2007 0 0 2007052257  389.5654437656637 1091
        5 0   4 25 0 0 2007 0 1 2007052262   299.243383402965 1091
        5 0   . 22 0 0 2007 0 1 2007052299  437.2131192594462 1091
        5 0   . 25 1 1 2007 0 1 2007052314  218.6065596297231 1091
        5 0  10 24 1 1 2007 1 0 2007052345  715.7130115477634 1091
        5 0  20 23 0 0 2007 1 0 2007052347 1330.0465977555377 1091
        5 1   4 24 0 0 2007 1 1 2007052364 454.31016042313547 1091
        5 0   2 23 0 0 2007 0 0 2007052406 484.15020111674903 1091
        5 0   8 24 0 0 2007 1 1 2007052454  171.0503765263934 1091
        5 0   . 23 0 0 2007 1 1 2007052500  603.7698662494778 1091
        5 0   . 31 1 1 2007 0 1 2007052502 208.56175212432785 1092
        5 1   . 29 1 1 2007 1 1 2007052515 164.96023620995132 1091
        5 0   9  . 0 0 2007 0 1 2007052536  74.81084585074125 1091
        5 1   1 29 1 1 2007 1 1 2007052543  681.4047692609373 1092
        5 0   . 20 0 0 2007 0 1 2007052563  437.2131192594462 1091
        5 1  40 22 1 1 2007 0 0 2007052564  97.39136094141593 1091
        5 0   . 37 1 1 2007 0 0 2007052572 333.41321600569074 1092
        5 0   . 27 0 0 2007 0 1 2007052637  149.6216917014825 1091
        5 0  60 25 0 0 2007 0 1 2007052652 139.05350731328585 1091
        5 1   . 40 0 0 2007 1 1 2007052661 227.15508021156774 1091
        5 0   . 23 0 0 2007 0 1 2007052696 311.31256843263947 1091
        5 1   .  . 0 0 2007 0 1 2007052697  149.6216917014825 1091
        5 0   . 35 1 1 2007 0 1 2007052721 139.05350731328585 1091
        5 1  60 21 0 0 2007 0 1 2007052729 139.05350731328585 1091
        5 1  60 22 0 0 2007 0 0 2007052736  389.5654437656637 1091
        5 0   . 26 0 0 2007 0 0 2007052770  389.5654437656637 1091
        5 0   . 26 0 0 2007 0 1 2007052816  278.1070146265717 1091
        5 1  40 23 0 0 2007 0 1 2007052824   299.243383402965 1091
        5 1   . 35 1 1 2007 1 1 2007052826   494.880708629854 1091
        5 0  12 32 0 0 2007 0 0 2007052830  726.1608584151636 1092
        5 0   6 33 0 0 2007 0 0 2007052843  389.5654437656637 1091
        5 0   . 21 0 0 2007 0 1 2007052869   299.243383402965 1091
        5 0  60 24 0 0 2007 0 1 2007052872  417.1235042486557 1092
        5 0   . 31 1 1 2007 0 1 2007052888 139.05350731328585 1091
        5 0   . 23 1 1 2007 0 1 2007052912   299.243383402965 1091
        5 0   2 36 1 1 2007 0 1 2007052916  149.6216917014825 1091
        5 0  16 26 0 0 2007 0 0 2007052954  726.1608584151636 1092
        5 1   . 19 0 0 2007 . 1 2007052971 164.96023620995132 1091
        5 0   . 37 0 0 2007 0 1 2007052974 144.67748219949118 1091
        5 1   4 20 0 0 2007 0 1 2007052994 139.05350731328585 1091
        5 0   . 22 0 0 2007 1 1 2007053029 329.92047241990264 1091
        5 0   . 43 1 1 2007 0 1 2007007653 128.57103511479022 1101
        5 0   . 19 0 0 2007 1 1 2007007769 136.25603636290631 1101
        5 1  16 24 0 0 2007 0 0 2007007813  326.0710300003729 1101
        5 0   . 35 1 1 2007 1 0 2007007859  653.1667749256062 1101
        5 0   . 29 0 0 2007 . 1 2007007971 1435.0290055373296 1101
        5 0   . 31 1 1 2007 . 1 2007008029 1435.0290055373296 1101
        5 0   . 20 1 1 2007 0 1 2007008041  114.8572539915274 1101
        5 0   . 31 1 1 2007 1 1 2007008076 282.57290192293124 1101
        5 0   . 35 0 0 2007 1 1 2007008092   424.539898087838 1102
        5 0  60 30 0 0 2007 0 0 2007008102  483.4425891173027 1102
        5 0   . 30 1 1 2007 0 1 2007008164  270.8513035241445 1101
        5 0   . 24 0 0 2007 0 1 2007008304 257.14207022958044 1101
        5 0   4 44 0 0 2007 1 0 2007008323  351.4961930954036 1101
        5 1   . 27 0 0 2007 0 1 2007008479 128.57103511479022 1101
        5 0   4 25 0 . 2007 0 0 2007008503   275.863709956193 1102
        5 0   . 30 . . 2007 0 1 2007013844 128.57103511479022 1101
        5 0  30 20 0 0 2007 0 1 2007021378  218.6065596297231 1091
        5 1   . 26 0 0 2007 1 1 2007022627 282.57290192293124 1101
        5 0   . 30 0 0 2007 1 1 2007022662  562.8856671240469 1101
        5 0   . 31 0 0 2007 1 1 2007022671 282.57290192293124 1101
        5 0   . 29 0 0 2007 1 0 2007022818 1306.3335498512124 1101
        5 1   4 30 0 0 2007 0 1 2007022865 123.58650262363595 1101
        5 0   . 27 0 0 2007 0 1 2007022913   180.567535682763 1101
        5 0   . 25 0 0 2007 0 1 2007023021  247.1730052472719 1101
        5 1   . 25 0 0 2007 0 0 2007023149 436.28555256719307 1101
        5 0   . 25 1 1 2007 0 1 2007023188   180.567535682763 1101
        5 0   . 25 0 1 2007 0 1 2007023241  114.8572539915274 1101
        5 1   . 25 1 1 2007 0 1 2007023417  247.1730052472719 1101
        5 0   . 30 0 0 2007 1 1 2007023474 136.25603636290631 1101
        5 0   . 35 1 1 2007 0 0 2007023477  550.8429907906016 1101
        5 0   . 41 1 1 2007 1 0 2007023525 1098.6094694813837 1101
        5 0   . 24 0 0 2007 0 1 2007023542 193.16620161818616 1102
        5 1   . 24 1 1 2007 1 0 2007023665 1182.3481872654381 1101
        5 0   . 29 0 0 2007 1 0 2007026638  312.0877526773313 1141
        5 0   . 31 0 0 2007 0 1 2007036875 144.67748219949118 1091
        5 0   . 32 . . 2007 0 1 2007037889   180.567535682763 1101
        5 0   . 30 0 0 2007 0 1 2007037999 123.58650262363595 1101
        5 0   1 24 0 0 2007 1 1 2007038023  380.1360011363403 1102
        5 1   8 19 1 1 2007 0 0 2007038206 183.61433026353382 1101
        5 0   . 24 1 1 2007 0 1 2007038211  119.5026191062061 1101
        5 0   . 34 0 0 2007 . 1 2007038269  423.8593528843969 1101
        5 0   . 32 0 0 2007 0 1 2007038291  119.5026191062061 1101
        5 0   . 46 1 1 2007 0 1 2007038384 123.58650262363595 1101
        5 1   .  . 0 0 2007 1 1 2007038637 136.25603636290631 1101
        5 0   . 32 0 0 2007 1 1 2007038641 412.97472976929186 1101
        5 0   . 31 0 0 2007 1 0 2007038673  274.4606860855031 1101
        5 1   2 25 0 0 2007 1 0 2007038805  351.4961930954036 1101
        5 0   . 28 1 1 2007 0 1 2007038874  119.5026191062061 1101
        5 0   . 27 0 0 2007 0 0 2007038976 218.14277628359653 1101
        5 0   . 25 0 . 2007 1 1 2007038997 253.01775745998273 1101
        2 0   . 26 1 1 2007 1 1 2007050245  667.9405844068672 1062
        5 0   . 32 1 1 2007 0 1 2007052891  149.6216917014825 1091
        5 0   4 31 0 0 2007 1 1 2007053316 141.28645096146562 1101
        5 0   . 19 0 0 2007 1 1 2007053547  545.0241454516253 1101
        5 0   8 33 0 0 2007 1 1 2007053564   249.354907402688 1101
        5 0   . 27 0 0 2007 0 1 2007053597  371.3547963858237 1102
        5 0   4 26 1 1 2007 0 1 2007053708  229.7145079830548 1101
        5 0   . 33 0 0 2007 0 1 2007053770 257.14207022958044 1101
        5 0   1 32 1 1 2007 0 0 2007053820 367.22866052706763 1101
        5 0   .  . 0 0 2007 0 1 2007053933 123.58650262363595 1101
        5 0   . 39 0 0 2007 0 0 2007053964 163.03551500018645 1101
        3 0   . 26 1 1 2007 1 1 2007002395 261.28411911296575 1031
        3 0   . 15 1 1 2007 1 1 2007002427 391.92617866944863 1031
        3 0   .  . 0 0 2007 1 0 2007002449  1252.510420644777 1031
        3 0   . 30 0 0 2007 1 0 2007002486  337.0139614948305 1031
        3 1  18 31 1 1 2007 0 0 2007002522  308.5206051766755 1031
        2 0   . 27 0 0 2007 1 1 2007003382  600.9730495633917 1051
        2 0   . 32 1 1 2007 1 0 2007003742  946.7633978545607 1051
        3 0   . 33 0 0 2007 0 1 2007009279  61.03836362593748 1121
        3 1  45 21 0 0 2007 1 0 2007009306  373.5898552628225 1121
        3 0   . 20 0 0 2007 1 1 2007009385 265.02837184458514 1121
        3 0   . 27 0 1 2007 0 1 2007009412  61.03836362593748 1121
        3 0   . 34 1 1 2007 0 1 2007009537 131.35466196947084 1121
        3 0   . 26 0 0 2007 0 0 2007009645  171.0020672768487 1121
        3 0   . 25 0 0 2007 0 0 2007009718  171.0020672768487 1121
        3 1   . 24 0 0 2007 0 1 2007009729  65.67733098473542 1121
        3 0   . 22 0 0 2007 1 1 2007009770  72.41027627529331 1121
        3 1   . 38 0 0 2007 0 0 2007009849 195.15560172429846 1121
        3 1   . 22 1 1 2007 0 1 2007009943  98.19003713568658 1122
        3 1   . 19 1 1 2007 1 0 2007009990 157.08324045449376 1121
        1 1   4 26 0 0 2007 0 0 2007013877 367.22866052706763 1101
        3 0   . 33 1 1 2007 1 1 2007016608 478.16213553782256 1031
        3 0   .  . 0 0 2007 0 1 2007016721 173.12785092181664 1031
        3 0   . 21 0 0 2007 1 1 2007016791 478.16213553782256 1031
        3 0 180 23 1 1 2007 0 0 2007016971  308.5206051766755 1031
        3 0   8 25 1 1 2007 1 0 2007024609 157.08324045449376 1121
        3 0   . 37 0 0 2007 0 1 2007024647  61.03836362593748 1121
        3 0   3 29 1 1 2007 0 0 2007024713  231.8543694619741 1121
        3 0   8 30 0 0 2007 0 1 2007024732 131.35466196947084 1121
        3 0   . 27 0 0 2007 1 1 2007024789 199.42214552265264 1121
        3 1   . 62 1 1 2007 1 1 2007024850 289.64110510117325 1121
        3 0   . 41 0 0 2007 0 1 2007025000 196.38007427137316 1122
        3 0   . 21 0 0 2007 0 1 2007025015  61.03836362593748 1121
        
        
        end
        label values mscode metroarea
        label def metroarea 1 "center city of MSA", modify
        label def metroarea 2 "outside center city but inside county w/ center city", modify
        label def metroarea 3 "inside suburban county of MSA", modify
        label def metroarea 5 "not in MSA", modify
        label values actlim2 yesnobinary
        label values disabilitya yesnobinary
        label def yesnobinary 0 "no", modify
        label def yesnobinary 1 "yes", modify
        label values primeage2 page2
        label def page2 0 "oldage", modify
        label def page2 1 "primeage", modify
        label values gender gender
        label def gender 0 "male", modify
        label def gender 1 "female", modify
        Thank you again in advance for any help you can provide.

        Comment


        • #5
          This will get you something fairly close to what you want:

          Code:
          svyset _psu [pweight = _finalwt], strata(_ststr)
          
          foreach y of numlist /*2001*/ 2007 /*2015*/  {
              foreach v of varlist bmi drinkingvol actlim2 disabilitya {
                  svy, subpop(if year == `y'): mean `v', over(primeage2)
                  matrix T = r(table)
                  matrix list T
                  matrix M = nullmat(M), T
              }
          }
          
          matrix M = M'
          matrix M = M[1..., 1..6]
          matrix list M
          Notes:

          1. Your example data contains only observations with year == 2007. You can uncomment the other years in the -foreach y of numlist...- command when you apply this to your real data.

          2. At least in the example data, many of the strata have singleton PSU, which is why you aren't seeing any standard errors, t statistics, etc. in the output. Again, assuming your real data is more ample, this problem will go away when you apply the code to your real data.

          3. This is what you are asking for except for the contents of the rowstubs. If it's important to you, you can fiddle with those using -matrix rownames-.

          Comment


          • #6
            I was (oddly enough) able to run the code once, and when I went to re-run it to verify the code worked, I received this error from Stata:

            Code:
            . foreach y of numlist 2001 2007 2015  {
              2.     foreach v of varlist bmi drinkingvol actlim2 disabilitya {
              3.         svy, subpop(if year == `y'): mean `v', over(primeage2)
              4.         matrix T = r(table)
              5.         matrix list T
              6.         matrix M = nullmat(M), T
              7.     }
              8. }
            (running mean on estimation sample)
            
            Survey: Mean estimation
            
            Number of strata =     333      Number of obs   =      414,513
            Number of PSUs   = 414,513      Population size =  300,065,242
                                            Subpop. no. obs =      182,862
                                            Subpop. size    =  177,701,615
                                            Design df       =      414,180
            
                   oldage: primeage2 = oldage
                 primeage: primeage2 = primeage
            
            --------------------------------------------------------------
                         |             Linearized
                    Over |       Mean   Std. Err.     [95% Conf. Interval]
            -------------+------------------------------------------------
            bmi          |
                  oldage |    26.5266          .             .           .
                primeage |   26.45845          .             .           .
            --------------------------------------------------------------
            Note: 1966 strata omitted because they contain no
                  subpopulation members.
            Note: Missing standard errors because of stratum with single
                  sampling unit.
            
            T[9,2]
                          bmi:       bmi:
                       oldage   primeage
                 b  26.526603  26.458453
                se          .          .
                 t          .          .
            pvalue          .          .
                ll          .          .
                ul          .          .
                df     414180     414180
              crit  1.9599697  1.9599697
             eform          0          0
            conformability error
            r(503);
            
            end of do-file
            
            r(503);
            The code, when it did run, returned what I wanted (so thank you), so I'm unsure why on the 2nd run through & subsequent ones (even when running the do-file from the beginning and -clear all-) wouldn't find themselves compatible. This just concerns me in case I'd like to run multiple matrices after one another (preferably without having to start the do-file from the beginning).

            Code when it worked:

            Code:
            . matrix list M
            
            M[24,6]
                                          b         se          t     pvalue         ll         ul
                      bmi:oldage  26.526603          .          .          .          .          .
                    bmi:primeage  26.458453          .          .          .          .          .
              drinkingvol:oldage  8.1811656          .          .          .          .          .
            drinkingvol:primeage  11.968695          .          .          .          .          .
                  actlim2:oldage  .25618141          .          .          .          .          .
                actlim2:primeage  .14146008          .          .          .          .          .
              disabilitya:oldage  .41010811          .          .          .          .          .
            disabilitya:primeage  .23887953          .          .          .          .          .
                      bmi:oldage  27.131845  .02498523  1085.9154          0  27.082875  27.180816
                    bmi:primeage  27.175765  .02668769  1018.2884          0  27.123458  27.228072
              drinkingvol:oldage  25.901954  .67376352  38.443688          0  24.581398   27.22251
            drinkingvol:primeage  23.046763  .40278288  57.218823          0   22.25732  23.836205
                  actlim2:oldage  .29568769  .00196262     150.66          0  .29184103  .29953436
                actlim2:primeage  .14922961  .00151262  98.656359          0  .14626493   .1521943
              disabilitya:oldage  .38170945  .00333578  114.42874          0  .37517142  .38824747
            disabilitya:primeage  .18649348  .00269526  69.193044          0  .18121085  .19177611
                      bmi:oldage  27.630465          .          .          .          .          .
                    bmi:primeage  27.806822          .          .          .          .          .
              drinkingvol:oldage  13.069099          .          .          .          .          .
            drinkingvol:primeage  16.081799          .          .          .          .          .
                  actlim2:oldage    .299076          .          .          .          .          .
                actlim2:primeage   .1631626          .          .          .          .          .
              disabilitya:oldage  .43901278          .          .          .          .          .
            disabilitya:primeage  .27259872          .          .          .          .          .

            Comment


            • #7
              Actually, that happened to me when I was developing and testing the code, too. You need to run -clear*- before running the code. (Or, if there are some things you don't want to clear, at least run -matrix drop M- before any re-run

              What's happening is, when you run the code the first time, the matrix M does not exist, and the code creates it. WHen you re-run the code, the matrix M is already hanging around from the previous run, but when you reach -matrix M = nullmat(M), T- this is a problem because M, having been transposed during the first run of the code, is now the wrong shape to be adjoined to T. If you just get rid of matrix M before re-running the code, you will not have this problem.

              Comment


              • #8
                Clyde,

                Thank you so much for assisting with the code & providing a solution to the matrix error! I've worked very little with matrices in Stata, so that was immensely helpful.

                I added -matrix drop M- before the loop where the matrix is constructed and it works like a dream!

                Comment


                • #9
                  I'm providing a follow-up because I'm attempting to use -lincom- to test the difference between subpopulations and add these results to their own matrix.

                  So far, I've adjusted the code in this way:

                  Code:
                  matrix drop M //needed to clear matrix so it doesn't cause errors
                  matrix drop J
                  
                  foreach y of numlist 2001 2007 2015 {
                      foreach v of varlist bmi drinkingvol actlim2 disabilitya {
                          svy, subpop(if year == `y'): mean `v', over(primeage2)
                          matrix T = r(table)
                          matrix list T
                          matrix M = nullmat(M), T
                          
                          lincom [`v']oldage - [`v']primeage
                          matrix F = r(estimate)
                          matrix list F
                          matrix J = nullmat(J), F
                  
                      }
                  }
                  
                              matrix M = M'
                              matrix M = M[1..., 1..6]
                              matrix list M
                              
                              matrix J = J'
                              matrix J = J[1..., 1..1]
                              matrix list J
                  Which yields a matrix with no conformability errors. However, it also yields an estimates matrix of all estimates in the loop (including those from -svy:means-).


                  Code:
                  .                         matrix list J
                  
                  J[75,1]
                              r1
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1           .
                  c1   .06814961
                  c1  -3.7875294
                  c1   .11472133
                  c1   .17122858
                  c1  -.04391942
                  c1   2.8551917
                  c1   .14645808
                  c1   .19521597
                  c1  -.17635668
                  c1  -3.0127007
                  c1   .13591339
                  c1   .16641406
                  c1   .06814961
                  c1  -3.7875294
                  c1   .11472133
                  c1   .17122858
                  c1  -.04391942
                  c1   2.8551917
                  c1   .14645808
                  c1   .19521597
                  c1  -.17635668
                  c1  -3.0127007
                  c1   .13591339
                  c1   .16641406
                  c1   .06814961
                  c1  -3.7875294
                  c1   .11472133
                  c1   .17122858
                  c1  -.04391942
                  c1   2.8551917
                  c1   .14645808
                  c1   .19521597
                  c1  -.17635668
                  c1  -3.0127007
                  c1   .13591339
                  c1   .16641406
                  c1   .06814961
                  c1  -3.7875294
                  c1   .11472133
                  c1   .17122858
                  c1  -.04391942
                  c1   2.8551917
                  c1   .14645808
                  c1   .19521597
                  c1  -.17635668
                  c1  -3.0127007
                  c1   .13591339
                  c1   .16641406
                  c1   .06814961
                  c1  -3.7875294
                  c1   .11472133
                  c1   .06814961
                  c1  -3.7875294
                  c1   .11472133
                  c1   .17122858
                  c1  -.04391942
                  c1   2.8551917
                  c1   .14645808
                  c1   .19521597
                  c1  -.17635668
                  c1  -3.0127007
                  c1   .13591339
                  c1   .16641406

                  Any advice on how to constrict the matrix to just show the estimates for -lincom- calculations would be helpful! Thank you again in advance!

                  Comment


                  • #10
                    I don't understand what the problem is. The new matrix J contains only the estimates from the -lincom commands. There is one such estimate for each combination of year and outcome variable. I don't get what you are expecting here. If you don't want to see the actual means of the variables themselves, just skip the -matrix list M- statement.

                    Comment


                    • #11
                      I guess I'm really trying to do is label the column names so that the results appear more like the output generated by the matrix for -svy: means-. I apologize for not being more clear.

                      I've attempted to use the -matrix coleq- command to label each output according the -lincom- it is associated with, but I get a [1, 1] matrix instead of the full matrix of -lincom- results:

                      Code:
                      //MAKING ANOTHER MATRIX FOR LINCOM
                      
                      matrix drop M //needed to clear matrix so it doesn't cause errors
                      matrix drop J
                      
                      foreach y of numlist 2001 2007 2015 {
                          foreach v of varlist bmi drinkingvol actlim2 disabilitya {
                              svy, subpop(if year == `y'): mean `v', over(primeage2)
                              matrix T = r(table)
                              matrix list T
                              matrix M = nullmat(M), T
                              
                              lincom [`v']oldage - [`v']primeage
                              matrix F = r(estimate)
                              matrix list F
                              matrix J = nullmat(J), F
                      
                          }
                      }
                                  
                                  foreach v of varlist bmi drinkingvol actlim2 disabilitya {
                                  matrix coleq J = "[`v']oldage - [`v']primeage"
                                  }
                                  
                                  matrix rownames J = lincomresult
                                  
                                  matrix J = J'
                                  matrix J = J[1..., 1..1]
                                  matrix list J
                      Result:

                      Code:
                      .                         foreach v of varlist bmi drinkingvol actlim2 disabilitya {
                        2.                         matrix coleq J = "[`v']oldage - [`v']primeage"
                        3.                         }
                      
                      .                         
                      .                         matrix rownames J = lincomresult
                      
                      .                         
                      .                         matrix J = J'
                      
                      .                         matrix J = J[1..., 1..1]
                      
                      .                         matrix list J
                      
                      symmetric J[1,1]
                                                                    lincomresult
                      [disabilitya]oldage - [disabi~t:lincomresult     .06814961
                      .

                      Comment


                      • #12
                        I think you want this:

                        Code:
                        capture matrix drop M //needed to clear matrix so it doesn't cause errors
                        capture matrix drop J
                        
                        local vars_of_interest    bmi drinkingvol actlim2 disabilitya
                        
                        foreach y of numlist /*2001*/ 2007 /*2015*/ {
                            foreach v of varlist `vars_of_interest' {
                                svy, subpop(if year == `y'): mean `v', over(primeage2)
                                matrix T = r(table)
                                matrix list T
                                matrix M = nullmat(M), T
                                
                                lincom [`v']oldage - [`v']primeage
                                matrix F = r(estimate)
                                matrix list F
                                matrix J = nullmat(J), F
                        
                            }
                        }
                                    
                                    matrix M = M'
                                    matrix M = M[1..., 1..6]
                                    matrix list M
                                    
                                    
                                    matrix J = J'
                                    matrix J = J[1..., 1..1]
                                    matrix rownames J = `vars_of_interest'
                                    matrix colnames J = lincom_result
                                    matrix list J
                        Added or changed code shown in italics.

                        Notice that I have put your list of variables into a local macro, and all subsequent references to them are via this macro. This is to assure that the names in matrix J will always be the same as the ones in matrix M, and in the right order. (And if at some point you want to revise this code to use different variables, you only have to change the one line where local macro vars_of_interest gets defined.

                        Comment


                        • #13
                          A tangential issue: You are missing standard errors because of strata with members of just one of the subpopulations. To get work around this, merge those problem strata, as follows:
                          1. Identify those strata.
                          2. Create a new stratum variable:
                          Code:
                          clonevar newstr = _ststr
                          replace newstr=  -1 if inlist(_str,......)  // add  stratum ids of the problem strata to inlist
                          Then svyset with newstr as the stratum variable and repeat the analysis.
                          Last edited by Steve Samuels; 19 Jul 2018, 12:20.
                          Steve Samuels
                          Statistical Consulting
                          [email protected]

                          Stata 14.2

                          Comment


                          • #14
                            Thank you for the adjustments to the -lincom- matrix. Now when I run it, I get what I want (using the exact code Clyde posted, so thank you):

                            Code:
                                 matrix list J
                            
                            J[4,1]
                                         lincom_res~t
                                    bmi    -.04391942
                            drinkingvol     2.8551917
                                actlim2     .14645808
                            disabilitya     .19521597
                            Is there an efficient way of identifying singleton problem strata? For example, there's 1295 problem strata in my set for one indicator variable in one year but 995 in another. Essentially, the number of problem strata are changing as the code for the -svy:means- and -lincom- loop is run, so I'm unsure how to identify the problem strata during -svyset- in such a way to address this problem and accurately -svyset- the entire data set.

                            Excerpt example:
                            Code:
                            Survey: Mean estimation
                            
                            Number of strata =   1,006      Number of obs   =      623,493
                            Number of PSUs   = 623,493      Population size =  415,087,631
                                                            Subpop. no. obs =      405,982
                                                            Subpop. size    =  199,414,171
                                                            Design df       =      622,487
                            
                                   oldage: primeage2 = oldage
                                 primeage: primeage2 = primeage
                            
                            --------------------------------------------------------------
                                         |             Linearized
                                    Over |       Mean   Std. Err.     [95% Conf. Interval]
                            -------------+------------------------------------------------
                            disabilitya  |
                                  oldage |   .3817094   .0033358      .3751714    .3882475
                                primeage |   .1864935   .0026953      .1812108    .1917761
                            --------------------------------------------------------------
                            Note: 1295 strata omitted because they contain no
                                  subpopulation members.
                            
                            T[9,2]
                                     disabilitya:  disabilitya:
                                          oldage      primeage
                                 b     .38170945     .18649348
                                se     .00333578     .00269526
                                 t     114.42874     69.193044
                            pvalue             0             0
                                ll     .37517142     .18121085
                                ul     .38824747     .19177611
                                df        622487        622487
                              crit     1.9599678     1.9599678
                             eform             0             0
                            
                             ( 1)  [disabilitya]oldage - [disabilitya]primeage = 0
                            
                            ------------------------------------------------------------------------------
                                    Mean |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                            -------------+----------------------------------------------------------------
                                     (1) |    .195216   .0042899    45.51   0.000     .1868079     .203624
                            ------------------------------------------------------------------------------
                            
                            symmetric F[1,1]
                                       c1
                            r1  .19521597
                            (running mean on estimation sample)
                            
                            Survey: Mean estimation
                            
                            Number of strata =   1,306      Number of obs   =      410,303
                            Number of PSUs   = 410,303      Population size =  215,791,291
                                                            Subpop. no. obs =      380,370
                                                            Subpop. size    =  198,547,756
                                                            Design df       =      408,997
                            
                                   oldage: primeage2 = oldage
                                 primeage: primeage2 = primeage
                            
                            --------------------------------------------------------------
                                         |             Linearized
                                    Over |       Mean   Std. Err.     [95% Conf. Interval]
                            -------------+------------------------------------------------
                            bmi          |
                                  oldage |   27.63047          .             .           .
                                primeage |   27.80682          .             .           .
                            --------------------------------------------------------------
                            Note: 995 strata omitted because they contain no subpopulation
                                  members.
                            Note: Missing standard errors because of stratum with single
                                  sampling unit.
                            
                            T[9,2]
                                          bmi:       bmi:
                                       oldage   primeage
                                 b  27.630465  27.806822
                                se          .          .
                                 t          .          .
                            pvalue          .          .
                                ll          .          .
                                ul          .          .
                                df     408997     408997
                              crit  1.9599698  1.9599698
                             eform          0          0

                            Comment


                            • #15
                              If you run this:
                              Code:
                              by _ststr _psu, sort: gen psu_count = (_n == 1)
                              by _ststr (_psu): replace psu_count = sum(psu_count)
                              by _ststr (_psu): replace psu_count = psu_count[_N]
                              the variable psu_count will give you the number of psu's in each stratum. Those with psu_count == 1 are the offending ones.

                              As for dealing with them, if you have such a large number, that will be difficult to do. Ideally, you would consider each single-psu stratum in light of the original sampling design and reassign that psu to the stratum that is most similar (in terms of your study) to it. But to do this for roughly 1,000 such strata seems infeasible. I have heard of people doing things like creating a single new stratum and re-assigning all the psu's from the offending strata to that one. I have also heard of people reassigning all of the offending psu's to the stratum that has the largest number of psu's. I am not enough of a survey maven to say just how bad these approaches are, but my instinct is that they're not very good. Fortunately, Steve Samuels has also been following this thread, and he is far more knowledgeable about survey data analysis than I am. I hope he will chime in here.

                              Comment

                              Working...
                              X