简体   繁体   English

如何替换stata中多个变量的变量检查值

[英]How to replace variable checking values of many several variables in stata

I have a variable named math_subject with values 1 and 2. Now I want to replace the values in the variable math_subject = 0 if other 30 variables == 2. I am asking for a short way to do this.我有一个名为 math_subject 的变量,其值为 1 和 2。现在,如果其他 30 个变量 == 2,我想替换变量 math_subject = 0 中的值。我要求一种简短的方法来执行此操作。

replace math_subject = 0 if all 30 variables with different naming format equal == 2 (if and & condition for 30 variables)如果具有不同命名格式的所有 30 个变量都等于 == 2,则替换 math_subject = 0(如果和 & 条件适用于 30 个变量)

Thank you.谢谢你。

Here is a simplified example that use 5 instead of 30 variables, but you should be able to modify this example.这是一个使用 5 个变量而不是 30 个变量的简化示例,但您应该能够修改此示例。

clear
input double(math_subject var1 var2 var3 var4 var5)
2 1 1 1 2 0
1 2 2 2 2 2
1 0 1 2 0 1
2 0 2 0 0 2
2 2 2 0 1 2
2 1 2 2 1 2
1 2 2 2 2 2
2 0 0 2 0 1
1 2 1 0 1 1
2 1 0 1 0 2
end

* Count number of variables that equals 2 for each observation
egen count_twos = anycount(var1 var2 var3 var4 var5) , values(2)

* Set math_subject to 0 if count_twos equals the number of variables in anycount() above
replace math_subject = 0 if count_twos == 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM