计划在stata中产生svy比率

时间:2016-01-28 21:51:45

标签: stata

我编写了以下代码,以便在同一个表格中列出多个指标的平均结果:

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

结果应该是

Variable    Mean    SE      N

var1       0.538    0.015   9396

var2       0.191    0.009   9396

当我修改程序以适应"比率时出现问题"计算var1/var2的案例。

以下是修改后的代码:

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给了我这个错误

/ invalid name

有关如何修改代码的任何建议???

2 个答案:

答案 0 :(得分:3)

您的问题是您的syntax命令告诉Stata您的程序的参数是变量名称列表,var1/var2不是变量名称。您需要不同的语法语句。以下是您的问题及其解决方案的演示。

. 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

.

答案 1 :(得分:0)

现在有效。这是最终的代码

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