Hello everyone,
I am working with several binary variables (coded 0/1, with possible missing values) in Stata. I want to create a new variable that sums the number of 1s across a group of these indicators.
However, I need a small adjustment:
gen total_strengths = (var1 == 1) + (var2 == 1) + (var3 == 1) + (var4 == 1)
But this approach gives 0 even if all variables are missing, which is not what I want.
Is there a clean way to sum the 1s but get missing if all input variables are missing? Ideally something efficient if I have 10+ variables.
Thank you very much for your help!
Best regards,
I am working with several binary variables (coded 0/1, with possible missing values) in Stata. I want to create a new variable that sums the number of 1s across a group of these indicators.
However, I need a small adjustment:
- If all the variables are missing (i.e., no information available), I would like the sum to be recorded as missing (.), not 0.
- If some variables are 0 and some are 1, the sum should behave normally (counting the number of 1s).
- If all variables are 0 (no 1s, but observed), the sum should correctly be 0.
gen total_strengths = (var1 == 1) + (var2 == 1) + (var3 == 1) + (var4 == 1)
But this approach gives 0 even if all variables are missing, which is not what I want.
Is there a clean way to sum the 1s but get missing if all input variables are missing? Ideally something efficient if I have 10+ variables.
Thank you very much for your help!
Best regards,
Comment