Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Creating household id for groups of persons

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject Re: st: Creating household id for groups of persons Date Wed, 6 Jul 2011 10:29:11 -0400

```Hans Meier <mr.hans.meier@web.de>:

Maybe this is what you want?

clear all
input contract id
123  1
123  2
123  3
456  4
456  5
678  1
456  3
789  6
789  7
456  8
end
g long obs=_n
egen long i=group(id)
la var i "Person id from 1 to M"
egen long gp=group(contract)
la var gp "Contract id from 1 to G"
bys i (gp):g long ct=sum(gp!=gp[_n-1])
la var ct "n distinct contract by id"
sort i ct
su i, mean
forv i=1/`r(max)' {
su ct if i==`i', mean
if r(max)==1 continue
loc max=r(max)
su gp if ct==1&i==`i', mean
loc g1=r(max)
forv j=2/`max' {
su gp if ct==`j'&i==`i', mean
replace gp=`g1' if gp==r(max)
}
}
sort obs
drop obs ct i
l, noo clean

On Wed, Jul 6, 2011 at 8:45 AM, Hans Meier <mr.hans.meier@web.de> wrote:
> Yes, now you got my question right.
> I don't know who lives in in which household, and I also don't have further information about this.
>
> But I assume, that if people have an insurance contract together, they are somehow connected and I define them as one household.
> (I look only at non-life insurance, no pension funds etc.)
>
> In my example, I define the persons from contract "123" (id's "1", "2", "3") as one household, let's say household A, and those in contract "456" (id's "4", "5") as another household B.
> Now, in contract "678", the id "1" tells me that this is the same person who is also in the contract "123", so I want this contract to be put in household A.
>