M Elkasabi
M Elkasabi

Reputation: 19

Program to produce svy Ratio in stata

I wrote the following code to tabulate the mean results for multiple indicators at the same table:

program RESULTS_MEANS
syntax varlist

tempname v se

local k : list sizeof varlist
display as txt %12s "Variable"      ///
    as txt %10s "Mean"      ///
    as txt %12s "SE"        ///
    as txt %12s "N"

forval i = 1/`k' {
    local var : word `i' of `varlist'
    quietly svy: mean `var'
    quietly estat size
    quietly estat effects
    matrix `v' = e(V)
    scalar `se' = sqrt(el(`v',1,1)) 

    display   as txt %12s "`var'"           ///
        " "   as res %9.0g _b[`var']        ///
        "   " as res %9.0g `se'             ///
        "   " as res %9.0g e(N)                         
}
end


RESULTS_MEANS var1 var2

The results should look like

Variable    Mean    SE      N

var1       0.538    0.015   9396

var2       0.191    0.009   9396

The problem appears when I modify the program to accommodate the "ratio" case to calculate var1/var2.

Here is the modified code:

program RESULTS_RATIOS
syntax varlist

tempname v se

local k : list sizeof varlist
display as txt %12s "Variable"      ///
    as txt %10s "Mean"      ///
    as txt %12s "SE"        ///
    as txt %12s "N"

forval i = 1/`k' {
local var : word `i' of `varlist'
    quietly svy: ratio `var'
    quietly estat size
    quietly estat effects
    matrix `v' = e(V)
    scalar `se' = sqrt(el(`v',1,1)) 

    display   as txt %12s "`var'"           ///
        " "   as res %9.0g _b[`var']        ///
        "   " as res %9.0g `se'             ///
        "   " as res %9.0g e(N)                         
}
end


RESULTS_RATIOS (var1/var2)

Stata gives me this error

/ invalid name

Any suggestions on how to modify the code???

Upvotes: 0

Views: 125

Answers (2)

M Elkasabi
M Elkasabi

Reputation: 19

It works now. here's the final code

program RESULTS_RATIOS
syntax anything

tempname v se

local k : list sizeof anything
display as txt %12s "Variable"      ///
    as txt %10s "Mean"      ///
    as txt %12s "SE"        ///
    as txt %12s "N"

forval i = 1/`k' {
local var : word `i' of `anything'
    quietly svy: ratio `var'
    quietly estat size
    quietly estat effects
    matrix `v' = e(V)
    scalar `se' = sqrt(el(`v',1,1)) 

    display   as txt %12s "`var'"           ///
        " "   as res %9.0g _b[`var']        ///
        "   " as res %9.0g `se'             ///
        "   " as res %9.0g e(N)                         
}
end

RESULTS_RATIOS var1/var2 var3/var4

Upvotes: 0

user4690969
user4690969

Reputation:

Your problem is that your syntax command tells Stata that the arguments to your program are a list of variable names, and var1/var2 is not a variable name. You need a different syntax statement. Below is a demonstration of your problem and its solution.

. clear

. set obs 1
number of observations (_N) was 0, now 1

. generate var1 = 1

. generate var2 = 2

. program RR
  1. syntax varlist
  2. display "parsed list: `varlist'"
  3. end

. RR var1 var2
parsed list: var1 var2

. RR var3
variable var3 not found
r(111);

. RR var1/var2
/ invalid name
r(198);

. program SS
  1. syntax anything
  2. display "parsed list: `anything'"
  3. end

. SS var1 var2
parsed list: var1 var2

. SS var3
parsed list: var3

. SS var1/var2
parsed list: var1/var2

.

Upvotes: 3

Related Questions