Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Creating variables with ordering information


From   Fred Wolfe <[email protected]>
To   [email protected], [email protected]
Subject   Re: st: Creating variables with ordering information
Date   Sun, 06 Aug 2006 08:12:25 -0500

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 |
     +------------------------------------------+



At 04:19 PM 8/5/2006, Hui Wang wrote:
Hi,

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"? Thank you very much.

Jackie

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

*
*   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/

Fred Wolfe
National Data Bank for Rheumatic Diseases
Wichita, Kansas
Tel +1 316 263 2125
[email protected]


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index