如何跨列添加

How to add across columns

假设我有这个数据:

clear
set more off

input ///
str15(s1 s2 s3 s4)
a "b" "b" "c" 
b "b" "" "a"
c "c" "" ""
d "f" "" "g"
e "" "" ""
end

我想逐行查找跨列的非缺失值的数量。我试试:

gen sum = s1!="" + s2!="" + s3!="" + s4!=""

但这给出了类型不匹配错误。我做错了什么?

但是,我可以把它全部写出来并且有效:

gen x=s1!=""
gen y=s2!=""
gen z=s3!=""
gen q=s4!=""
gen sum1=x + y + z + q

我相信你想要这样的东西:

gen sum2 = !missing(s1) + !missing(s2) + !missing(s3) + !missing(s4)

或者更紧凑的东西:

 egen sum3 = rownonmiss(s1 s2 s3 s4), strok

egen 代表扩展生成,通常是开始查找的好地方,如果第一个失败则紧接着 egenmore

但是,我不太清楚为什么您的代码会因字符串而失败。它似乎适用于数字变量。

问题是运算符优先级。以下作品:

gen sum = (s1!="") + (s2!="") + (s3!="") + (s4!="")

来自 Stata 用户指南

The order of evaluation (from first to last) of all operators is ! (or ~), ^, - (negation), /, *, - (subtraction), +, != (or ~=), >, <, <=, >=, ==, &, and |.

不过,我更喜欢罗伯托和迪米特里rownonmiss的推荐。