Hi,
I am quite new in using loop in Stata, and I find of great help this forum. I did some search on this topic but could not find an answer suited to my challenge.

I am using Stata 11.2 for determining the use of pesticide use with survey data. Questions where asked to identify pests to men and women separately (2 groups).
Gender is B4_n (being n number of household member), 0 is for male and 1 for female.
G1 is the number of household member for the first group of questions
I1 is the number of household member for the second group of questions (opposite gender).

I am trying to tell Stata to count the correct values of identifying insects for the two groups and create a variable for men and women .

egen knows_pests_first=anycount(G8a G8c G8f G8h G8i G8m G8n), values(1)
replace knows_pests_first=. if G1==.
*Same for other gender
egen knows_pests_other=anycount(I4a I4c I4f I4h I4i I4m I4n), values(1)
replace knows_pests_other=. if I1==.

*Sort into male and female respondents
local p=G1
local s=I1
gen man_knows_pests=knows_pests_first if B4_`p'==0
gen woman_knows_pests=knows_pests_first if B4_`p'==1
replace man_knows_pests=knows_pests_other if B4_`s'==0 & man_knows_pests==.
replace woman_knows_pests=knows_pests_other if B4_`s'==1 & woman_knows_pests==.


The command works for almost all data but gives some empty answers where there should be an observation, specially when the household number is >=3.


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte(B4_1 B4_2 B4_3 B4_4 B4_5 B4_6 B4_7 B4_8 B4_9 B4_10 B4_11 B4_12 B4_13 B4_14 B4_15 G1 G8a G8c G8f G8h G8i G8m G8n I1 I4a I4c I4f I4h I4i I4m I4n)
0 1 0 0 . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 0 1 1 1 1 1
0 1 0 1 0 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 0 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 0 1 . . . . . . . . . . 1 1 1 1 0 1 1 1 2 0 1 0 0 1 1 1
0 1 . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 2 1 2 1 2 2 2 1 2 1 2 1 1 2
0 1 0 0 1 . . . . . . . . . . 1 1 1 1 2 2 1 1 1 2 0 1 0 1 1 0
0 1 1 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1
0 1 1 0 0 1 0 . . . . . . . . 1 1 1 1 2 1 2 1 2 1 1 1 1 1 1 1
0 1 1 1 0 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 1 0 0 1 0 1
0 1 0 0 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1
0 1 1 1 1 . . . . . . . . . . 1 1 1 1 0 1 1 1 2 0 1 0 1 2 1 0
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1
0 1 1 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 2 2 2 1 1 1 2 1 2 1 2 1 1 1
0 1 1 0 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 0 1 0 1 1 . . . . . . . 1 1 1 1 2 1 1 1 2 0 0 0 2 2 1 1
0 1 0 0 0 1 . . . . . . . . . 1 1 2 1 1 2 1 1 2 1 1 2 1 1 1 1
0 1 1 1 0 1 . . . . . . . . . 1 1 2 2 0 1 0 1 3 1 1 2 1 2 1 1
0 1 0 1 . . . . . . . . . . . 1 2 1 2 1 1 1 1 2 2 1 2 1 1 1 1
0 1 0 0 0 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 1 . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 0 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 0 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 2 1 1 2 1 1 1 1 2 1 1 1 1 1 1
0 1 0 0 0 . . . . . . . . . . 1 1 1 2 1 1 1 1 2 2 1 2 1 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 0 1 2 1
0 1 1 1 0 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 1 0 . . . . . . . . . 1 1 2 1 2 1 1 1 1 1 1 1 1 0 1 1
0 1 1 0 1 . . . . . . . . . . 1 1 1 1 1 1 0 2 2 1 0 1 0 1 0 2
0 1 0 . . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1
0 1 1 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 0 1 0 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 . . . . . . . .
0 1 0 1 . . . . . . . . . . . 1 1 1 0 1 1 1 1 2 0 0 1 0 2 0 0
0 1 0 0 . . . . . . . . . . . 1 1 2 1 1 1 2 2 2 1 0 0 2 0 1 0
0 1 0 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 0 1 . . . . . . . . . . 1 1 1 1 2 1 1 1 2 0 1 1 1 0 0 1
0 1 0 1 1 0 . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 2
0 1 0 1 0 1 0 . . . . . . . . 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1
0 1 0 1 0 0 . . . . . . . . . 1 0 2 1 1 1 1 1 2 0 1 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 1 . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 2 2 0 1 1 1
0 1 0 0 0 . . . . . . . . . . 1 0 1 1 1 1 1 1 2 0 1 1 1 1 1 1
0 1 . . . . . . . . . . . . . 1 1 1 1 1 0 1 1 2 1 0 1 0 1 1 1
0 1 1 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 0 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 0 0 0 0 . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 1 0 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 0 1 1 . . . . . . . . . 1 2 1 1 1 1 1 1 5 1 1 1 1 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 2 2 1 1 1 2 1 0 2 2 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 0 0 1 1 0
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 3 1 0 1 1 1 1 1
0 1 1 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 0 1 1 1 1 1 . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 . . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 0 1 1 1 1 1
0 1 1 0 . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 1 0 1 1 1 1 1 2 1 0 0 1 0
0 1 0 0 0 1 . . . . . . . . . 1 1 1 2 1 2 1 1 2 1 2 0 1 1 1 1
0 1 0 1 0 . . . . . . . . . . 1 0 1 1 1 1 1 1 2 0 1 0 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 1 0 1 1 1 2 1 1 1 1 0 1 1
0 1 0 . . . . . . . . . . . . 1 0 1 1 1 1 1 1 2 0 1 1 1 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 . . . . . . . . . . . . 3 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2
0 1 0 . . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 1 0 1 1 1 1
0 1 0 . 0 1 . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 2 2 0 1 1 2 0 1 1 2 1 1 1
0 1 0 1 0 0 1 . . . . . . . . 1 1 1 1 1 1 1 1 2 1 0 1 2 1 1 0
0 1 0 1 0 . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 1 2 1 1 1 1 2 0 1 0 1 0 0 0
0 1 0 . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 1 0 . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 1 1 1 0 1 1
0 1 0 1 1 1 . . . . . . . . . 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 0 1 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 2 1 1 0 1 2
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1
0 1 1 . . . . . . . . . . . . 1 2 1 1 1 1 1 1 1 2 1 2 0 1 1 1
0 1 0 0 1 . . . . . . . . . . 1 1 1 1 0 1 1 1 2 1 1 1 1 1 1 1
0 1 1 0 . . . . . . . . . . . 1 2 2 0 1 1 1 1 2 1 1 1 1 1 1 1
0 1 . . . . . . . . . . . . . 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1
0 1 1 1 . . . . . . . . . . . 1 1 2 1 1 1 1 1 2 1 2 1 2 1 1 1
0 1 0 . . . . . . . . . . . . 1 2 1 1 2 1 1 1 2 2 1 1 2 2 1 1
0 1 0 1 0 . . . . . . . . . . 1 1 2 1 1 1 1 1 2 1 1 2 1 1 1 1
0 1 0 . . . . . . . . . . . . 1 1 1 2 1 1 1 2 2 1 1 2 1 1 1 2
0 1 0 0 . . . . . . . . . . . 1 1 1 1 0 1 1 1 2 0 0 0 0 1 1 1
0 1 1 0 . . . . . . . . . . . 1 1 1 2 1 1 1 1 2 0 0 0 1 1 1 1
0 1 0 0 . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 0 1 1 1
end
label values B4_1 B4
label values B4_2 B4
label values B4_3 B4
label values B4_4 B4
label values B4_5 B4
label values B4_6 B4
label values B4_7 B4
label values B4_8 B4
label values B4_9 B4
label values B4_10 B4
label values B4_11 B4
label values B4_12 B4
label values B4_13 B4
label values B4_14 B4
label values B4_15 B4
label def B4 0 "Male", modify
label def B4 1 "Female", modify
label values G8a G8a
label def G8a 0 "Dont know", modify
label def G8a 1 "Can do damage", modify
label def G8a 2 "Helpful insect", modify
label values G8c G8c
label def G8c 1 "Can do damage", modify
label def G8c 2 "Helpful insect", modify
label values G8f G8f
label def G8f 0 "Dont know", modify
label def G8f 1 "Can do damage", modify
label def G8f 2 "Helpful insect", modify
label values G8h G8h
label def G8h 0 "Dont know", modify
label def G8h 1 "Can do damage", modify
label def G8h 2 "Helpful insect", modify
label values G8i G8i
label def G8i 0 "Dont know", modify
label def G8i 1 "Can do damage", modify
label def G8i 2 "Helpful insect", modify
label values G8m G8m
label def G8m 0 "Dont know", modify
label def G8m 1 "Can do damage", modify
label def G8m 2 "Helpful insect", modify
label values G8n G8n
label def G8n 1 "Can do damage", modify
label def G8n 2 "Helpful insect", modify
label values I4a I4a
label def I4a 0 "Dont know", modify
label def I4a 1 "Can do damage", modify
label def I4a 2 "Helpful insect", modify
label values I4c I4c
label def I4c 0 "Dont know", modify
label def I4c 1 "Can do damage", modify
label def I4c 2 "Helpful insect", modify
label values I4f I4f
label def I4f 0 "Dont know", modify
label def I4f 1 "Can do damage", modify
label def I4f 2 "Helpful insect", modify
label values I4h I4h
label def I4h 0 "Dont know", modify
label def I4h 1 "Can do damage", modify
label def I4h 2 "Helpful insect", modify
label values I4i I4i
label def I4i 0 "Dont know", modify
label def I4i 1 "Can do damage", modify
label def I4i 2 "Helpful insect", modify
label values I4m I4m
label def I4m 0 "Dont know", modify
label def I4m 1 "Can do damage", modify
label def I4m 2 "Helpful insect", modify
label values I4n I4n
label def I4n 0 "Dont know", modify
label def I4n 1 "Can do damage", modify
label def I4n 2 "Helpful insect", modify
I hope the dataex is correct.

Many thanks in advance.