I want to generate a multiple indicator based on the values of the variable "handwashing_five_moments". For example, if the input = "1, 2, 5, 10" , I want each of the number(since it is category) to have its own separate dummy variable. My current result is as following:
I have run the following code:
The issue is that it is replacing the value for handwashing_1 == 1 even where the handwashing_five_moments does not have "1" and contains "10". How can I fix this?
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str18 handwashing_five_moments float(handwash_0 handwash_1 handwash_2 handwash_3 handwash_4 handwash_5 handwash_6 handwash_7 handwash_10) "1,2,3,5" 0 1 1 1 0 1 0 0 0 "2,5,6" 0 0 1 0 0 1 1 0 0 "2,3,5" 0 0 1 1 0 1 0 0 0 "1,2,5,10" 0 1 1 0 0 1 0 0 1 "2,5,10" 0 1 1 0 0 1 0 0 1 end
Code:
foreach s in 0 1 2 3 4 5 6 7 8 9 10 { gen handwash_`s' = strpos(handwashing_five_moments, "`s'") > 0 , }
Comment