Note that this can be telescoped to something like
bysort HH_ID gender (Birth_order) : gen nsame = _n - 1
separate nsame, by(gender) veryshortlabel
Nick
[email protected]
Fred Wolfe
> Try this:
>
> gen nboy = gender == "M"
> gen ngirl = gender == "F"
> qbys HH_ID (Birth_order) :replace ngirl = ngirl[_n-1] + ngirl if _n!=1
> replace ngirl = ngirl - 1 if gender =="F"
> qbys HH_ID (Birth_order) :replace nboy = nboy[_n-1] + nboy if _n!=1
> replace nboy = nboy - 1 if gender =="M"
> l ,sepby(HH)
>
> +------------------------------------------+
> | HH_ID Birth_~r gender nboy ngirl |
> |------------------------------------------|
> 1. | 1 1 F 0 0 |
> 2. | 1 2 M 0 1 |
> 3. | 1 3 M 1 1 |
> |------------------------------------------|
> 4. | 2 1 F 0 0 |
> 5. | 2 2 F 0 1 |
> 6. | 2 3 M 0 2 |
> 7. | 2 4 F 1 2 |
> 8. | 2 5 M 1 3 |
> +------------------------------------------+
>
Hui Wang
> >I have a data set with 3 variables listed as following:
> >
> >1) HH_id: Household id. To identify households.
> >2) Birth_order: Birth_order of individual in the household.
> To identify
> >individuals.
> >3) Gender: Sex of each individual.
> >
> >Now I want to creat two variables: nboy and ngirl, which
> counts, for each
> >individual in a given household, how many boy (girl) the
> household already
> >have when that individual was born.
> >
> >The following is a example:
> >
> >HH_id| Birth_order | Gender | nboy | ngirl|
> >1 | 1 | F | 0 | 0
> >1 | 2 | M | 0 | 1
> >1 | 3 | M | 1 | 1
> >2 | 1 | F | 0 | 0
> >2 | 2 | F | 0 | 1
> >2 | 3 | M | 0 | 2
> >2 | 4 | F | 1 | 2
> >2 | 5 | M | 1 | 3
> >
> >My question is, given "HH_id", "Birth_order", and "Gender",
> how to program
> >to generate "nboy" and "ngirl"?
*
* 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/