Hi Statalist,
Apologies for the vagueness of the question as I didn't know how to frame it better. Hopefully the details would convey the question better. Please consider the following example:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str4 var1 str8 var2 str6 var3
"hhid" "memberid" "headid"
"111"  "1"        "3"     
"111"  "2"        "3"     
"111"  "3"        "3"     
"112"  "1"        "1"     
"112"  "2"        "1"     
"112"  "3"        "1"     
"112"  "4"        "1"     
"113"  "1"        "4"     
"113"  "2"        "4"     
"113"  "3"        "4"     
end
now as can be seen, for hhid 113, headid has been wrongly entered as 4, while none of the members of that hh have an id 4. I suspect something like this has happened in my data and I want to identify that hhid for which this anomaly occurs. My understanding is the code would try to find, within each hhid whehter headid belongs to the set containing memberids, and if a headid doesn't belong to the corresponding set, it would flag the hhid.
However, i have been unable to figure out how to write this particular code and would appreciate any help from the community.

Thanks,
Titir