Hi,
I'm attempting to create a check system for some data I'm collecting to make sure it flags certain errors. The questionnaire consists of a bunch of yes/no (1/0) questions (hc002s1-hc002s11), and then a sort of stem variable (hc002) which is a string consisting of which questions a respondent answered "yes" to.
I'm hoping to loop through every variable and confirm that that specific question number appears in the stem variable if a respondent answered "yes". The problem I'm running into is that since double digit numbers also contain single digit numbers within them, the code will flag errors when there aren't any. For instance, if someone answered yes to the 10th question (hc002s10), but not the first (hc002s1), since 10 consists of the digits 1 & 0, the code flags it as an error since it sees the "1" within 10.
Is there anyway to be highly specific with the strpos function, so it doesn't match smaller substrings within it?
This is the code I created:
foreach var of varlist hc002s1-hc002s11 {
local n = substr("`var'", (strpos("`var'", "s") + 1), .)
assert `var' == 1 if strpos(hc002, "`n'")
}
David
I'm attempting to create a check system for some data I'm collecting to make sure it flags certain errors. The questionnaire consists of a bunch of yes/no (1/0) questions (hc002s1-hc002s11), and then a sort of stem variable (hc002) which is a string consisting of which questions a respondent answered "yes" to.
I'm hoping to loop through every variable and confirm that that specific question number appears in the stem variable if a respondent answered "yes". The problem I'm running into is that since double digit numbers also contain single digit numbers within them, the code will flag errors when there aren't any. For instance, if someone answered yes to the 10th question (hc002s10), but not the first (hc002s1), since 10 consists of the digits 1 & 0, the code flags it as an error since it sees the "1" within 10.
Is there anyway to be highly specific with the strpos function, so it doesn't match smaller substrings within it?
This is the code I created:
foreach var of varlist hc002s1-hc002s11 {
local n = substr("`var'", (strpos("`var'", "s") + 1), .)
assert `var' == 1 if strpos(hc002, "`n'")
}
David
Code:
clear input int(hc002s1 hc002s2 hc002s3 hc002s4 hc002s5 hc002s6 hc002s7 hc002s8 hc002s9 hc002s10 hc002s11) str21 hc002 0 1 1 0 0 1 0 0 0 0 0 "2-3-6" 0 1 1 0 0 0 0 1 0 0 0 "2-3-8" 0 1 1 0 0 0 0 1 0 0 0 "2-3-8" 0 1 0 1 0 0 0 0 0 0 0 "2-4" 0 1 1 1 0 0 0 1 0 1 0 "2-4-3-8-10" 0 1 0 1 1 0 0 0 0 0 0 "2-4-5" 0 1 0 1 1 0 0 0 0 0 0 "2-4-5" 0 1 0 0 1 0 0 0 0 0 0 "2-5" 0 1 0 0 1 0 0 0 0 0 0 "2-5" 1 1 0 0 1 0 0 0 0 0 0 "2-5-1" 1 1 0 0 1 0 0 0 0 0 0 "2-5-1" 0 1 0 1 1 1 0 0 0 0 0 "2-5-4-6" 0 1 0 0 1 1 0 0 0 0 0 "2-5-6" 0 1 0 0 0 1 0 0 0 0 0 "2-6" 0 1 1 0 0 1 0 0 0 0 0 "2-6-3" 0 1 0 1 0 1 0 0 0 1 0 "2-6-4-10" 0 1 0 0 0 0 0 1 0 0 0 "2-8" 0 1 1 0 0 0 0 1 0 0 0 "2-8-3" 0 1 0 0 0 1 0 1 0 0 0 "2-8-6" 1 1 1 1 1 1 0 0 0 0 0 "3-1-2-4-5-6" 1 1 1 0 1 0 0 0 0 0 0 "3-1-2-5" end
Comment