Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Creating household id for groups of persons


From   Friedrich Huebler <fhuebler@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Creating household id for groups of persons
Date   Wed, 6 Jul 2011 12:54:52 -0400

Hans,

Would you mind elaborating? Why does Austin's solution take hours and
why is using SSC a problem?

Thank you for sharing the message you received privately. This is an
opportunity to remind list members that the Statalist FAQ discourages
private messages: "Much of the benefit of Statalist is that an answer
to a question might well interest other people. [...] it is usually
better to post directly to Statalist."

http://www.stata.com/support/faqs/res/statalist.html#private

Friedrich

On Wed, Jul 6, 2011 at 11:47 AM, Hans Meier <mr.hans.meier@web.de> wrote:
> Hello Austin and Robert,
>
> thank you for your solutions.
> I'm sure they would work, but I have a very large dataset, so Austins solution would take hours, and for Roberts solution I would have to use SSC.
>
> But another Stata user sent me this solution:
>
> Von: "Fernando Rios Avila" <f.rios.a@gmail.com>
> Gesendet: 06.07.2011 15:18:00
> An: "'Hans Meier'" <mr.hans.meier@web.de>
> Betreff: RE: st: Creating household id for groups of persons
>
>>Hi Hans,
>>I was playing around with a very small sample similar to yours, and come up with this small code.
>>Here hid3 would be the household id code.
>>
>> egen hid1=group (contract)
>> bysort id: egen hid2=min(hid)
>> bysort contract:egen hid3=min(hid2)
>>
>>Hope this is what u were looking for.
>>Best
>
>
> It works perfect, and very fast.
>
> Thank you Fernando!
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: "Robert Picard" <picard@netbox.com>
> Gesendet: 06.07.2011 16:50:42
> An: statalist@hsphsun2.harvard.edu
> Betreff: Re: st: Creating household id for groups of persons
>
>>Or get -group_id- from SSC. Using Austin's data:
>>
>>* --------------------- begin example ---------------------
>>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
>>
>>clonevar gid = id
>>group_id gid, match(contract)
>>
>>list , noobs clean
>>
>>* --------------------- begin example ---------------------
>>
>>
>>On Wed, Jul 6, 2011 at 10:29 AM, Austin Nichols <austinnichols@gmail.com> wrote:
>>> 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

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index