I have a small dataset, which is layout in long format.
What I want is to create a new variable "retain" with the rules as follows,
as long as there is a "1" on the state variable within id, then the value of retain should be equal to 1 within id.
if all values of state variable within id =0, then then the value of retain should be equal to 0 within id.
if all values of state variable within id =1, then then the value of retain should be equal to 1 within id.
Can someone help me to do this with Stata code?
Thank you!
clear
input str10 id byte (grade state)
1 1 0
1 2 0
1 3 0
2 1 0
2 1 1
2 2 0
3 1 0
3 2 0
3 2 1
4 1 0
4 2 0
4 2 1
4 3 0
end
What I want is to create a new variable "retain" with the rules as follows,
as long as there is a "1" on the state variable within id, then the value of retain should be equal to 1 within id.
if all values of state variable within id =0, then then the value of retain should be equal to 0 within id.
if all values of state variable within id =1, then then the value of retain should be equal to 1 within id.
Can someone help me to do this with Stata code?
Thank you!
clear
input str10 id byte (grade state)
1 1 0
1 2 0
1 3 0
2 1 0
2 1 1
2 2 0
3 1 0
3 2 0
3 2 1
4 1 0
4 2 0
4 2 1
4 3 0
end

Comment