statistics - how to gen variable = 1 if at least two dummy variables == 1 in Stata? -


i trying generate dummy variable = 1 if @ least 2 or more (out of seven) dummy variables == 1. tell me efficient way of doing this?

let's suppose indicator variables concerned (you "dummy variables", that's terminology over-used given disadvantages) x1 ... x7. definition taken values 1 or 0, except values may missing. logic summary want is

gen xs = (x1 + x2 + x3 + x4 + x5 + x6 + x7) >= 2 if (x1 + x2 + x3 + x4 + x5 + x6 + x7) < .  

that's not difficult type, given copy , paste replicate syntax sum. if qualifier segregates observations missing on of indicators, missing returned new variable. such observations reported having total x1 + x2 + x3 + x4 + x5 + x6 + x7 missing. missing treated arbitrarily large in stata, , greater 2, explains why simpler code

gen xs = (x1 + x2 + x3 + x4 + x5 + x6 + x7) >= 2  

would bite if missings present.

if want more complicated rule, may find reaching egen functions rowtotal(), rowmiss(), , forth. see egen.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -