I am working through some data cleaning and I was wondering if there is some obvious more efficient way to replace 2 variables at once.
Very basic example, but in this case I only want one observation across the 3 levels of this variable, being the highest level (3) for each id. In this case I want to replace the var_level2 for id==2 to 0, and then replace the var_total for id==2 to 1 to continue exploring the data.
The way I have been going about it would be (once I already know which one I need to remove):
and then I would like to immediately change that var_total to 1 for id==2.
The two ways I can think of would be creating a temporary dummy variable to identify those subjects, change the 2 variables, and then drop the dummy variable, or just drop var_total and re-run the code to generate var_total again now that the values have changed. But both these options seem inefficient to me.
I hope this makes sense, I know that both these options would work, but ultimately I am trying to get better at Stata so I wanted to ask if there is a more efficient way to do this!
Thank you.
| id | var_level1 | var_level2 | var_level3 | var_total |
| 1 | 1 | 0 | 0 | 1 |
| 2 | 0 | 1 | 1 | 2 |
| 3 | 0 | 1 | 0 | 1 |
The way I have been going about it would be (once I already know which one I need to remove):
Code:
tab id if var_level2==1 & var_total==2 replace var_level2 = 0 if var_level2==1 & var_total==2
The two ways I can think of would be creating a temporary dummy variable to identify those subjects, change the 2 variables, and then drop the dummy variable, or just drop var_total and re-run the code to generate var_total again now that the values have changed. But both these options seem inefficient to me.
I hope this makes sense, I know that both these options would work, but ultimately I am trying to get better at Stata so I wanted to ask if there is a more efficient way to do this!
Thank you.

Comment