Dear sir/mam,
I have a HouseHold roster with question Q1-name, Q2-sex, Q3-age and out of roaster I have Q5- Respondent age is to be entered. I need a logic which will check the age entered in Q5 is matching with any age entered in Q3 if not then errmsg should appear.
I am trying creating loop but the errmsg appears for every occurrence of Q3 which is not equal to the number entered.
Loop!
-
justinlakier
- Posts: 238
- Joined: November 21st, 2022, 4:41 pm
Re: Loop!
Hello,
It sounds like you are looping over every Q3 and comparing it to the singly-occurring value of Q5. If it does not match, you give an error message. As you have seen, this will give the error message for every value of Q3 that does not match Q5. Instead of giving an error message immediately when you find a Q3 that does not match Q5, set a counter for when you do find a match, and then wait until after the loop is over to see if there are any matches, with an error message if no matches were found. See below for an example.
Hope this helps,
Justin
It sounds like you are looping over every Q3 and comparing it to the singly-occurring value of Q5. If it does not match, you give an error message. As you have seen, this will give the error message for every value of Q3 that does not match Q5. Instead of giving an error message immediately when you find a Q3 that does not match Q5, set a counter for when you do find a match, and then wait until after the loop is over to see if there are any matches, with an error message if no matches were found. See below for an example.
numeric matches=0;
for roster
if Q3 = Q5 then
matches = matches + 1;
endif;
endfor;
if matches = 0 then
errmsg("no matches were found during the loop");
endif;
for roster
if Q3 = Q5 then
matches = matches + 1;
endif;
endfor;
if matches = 0 then
errmsg("no matches were found during the loop");
endif;
Hope this helps,
Justin