Hello all, I want to separate multiple responses of numbers into binary variables. However, my attempt duplicates instances. For instance, if I want to extract number 1 from a multiple response of 1 5 10, the 1 in 10 also gets counted which is an error. How do I prevent this?
I have about 20 multiple response variables amongst several other variables, so I am looking for a way to automate the process. I have tried ODKmeta and ODKsplit but both aren't doing it well as their counts are not tallying with my manual counting.
Thanks.
Below is my code
Observe that the number of 1's in the original fpknow and the binary extraction of 1 does not tally in the result below.
Results
I have about 20 multiple response variables amongst several other variables, so I am looking for a way to automate the process. I have tried ODKmeta and ODKsplit but both aren't doing it well as their counts are not tallying with my manual counting.
Thanks.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str23 fpknow str14 preginfo "1 6 7 10 11" "96" "2 6 7 12 20" "1 3 5 10" "7" "1 3 5" "7" "1 3 5 10" "" "" end
Code:
local mult fpknow preginfo
foreach a of local mult {
local num "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 96 98"
display "`num'"
foreach b of local num {
cap drop `a'_`b'
cap confirm numeric variable `a'
if _rc{
local `b'
local `a'
gen `a'_`b' = 0 if `a'!=""
replace `a'_`b' = 1 if strmatch(`a', "*`b'*")
}
}
}
Results
Code:
. ta fpknow
fpknow | Freq. Percent Cum.
------------------------+-----------------------------------
1 6 7 10 11 | 1 25.00 25.00
2 6 7 12 20 | 1 25.00 50.00
7 | 2 50.00 100.00
------------------------+-----------------------------------
Total | 4 100.00
. ta fpknow_1
fpknow_1 | Freq. Percent Cum.
------------+-----------------------------------
0 | 2 50.00 50.00
1 | 2 50.00 100.00
------------+-----------------------------------
Total | 4 100.00

Comment