Reputation: 5
I have a variable in Stata 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.
replace math_subject = 0
if all 30 variables with different naming format == 2.
Upvotes: 0
Views: 73
Reputation: 3255
Here is a simplified example that uses 5 instead of 30 variables, but you should be able to modify this example.
clear
input byte(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
Upvotes: 3