Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: merging household and person data


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: merging household and person data
Date   Mon, 11 Nov 2002 09:32:13 -0000

Kompal Sinha

> I have two data files: first containing the household(HH) 
> data and second
> containing person wise data for the corresponding HH.
> I want to do the following:
> 1) In the person wise file which looks like this:
> HHID           religion
> 12345             1
> 12345             2
> 12345             3
> 12334             4
> 12334             4
> 12334             4
> 12334             5
> 
> I want to generate a new variable count such as it counts 
> the number of
> persons in a particular household and assigns a number to it, like:
> HHID           religion  count
> 12345             1        1
> 12345             2        2
> 12345             3        3
> 12334             4        1
> 12334             4        2
> 12334             4        3
> 12334             5        4

JT Sheu suggested 

> you can do the following:

> gen count=.
> sort hhid
> by hhid: replace count=_n

This is fine, but there's a small 
correction. We first note that what JT Sheu 
suggested can be telescoped: 

. bysort hhid : gen count = _n 

The small correction is that Kompal's 
example suggests a preference for 
sorting within -hhid- on -religion-. 
In that case Kompal needs 

. bysort hhid (religion) : gen count = _n 

This is a concise way of saying, all at once 

	-sort- on -hhid- and within -hhid- 
	on -religion- 

	-by hhid-, -generate- a new 
	variable which is the observation 
	number (within groups). 


Nick 
n.j.cox@durham.ac.uk 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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   |   What's new   |   Site index