Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: Calculate number of alive male and female children from a family dataset
From
"Martin Weiss" <[email protected]>
To
<[email protected]>
Subject
st: RE: Calculate number of alive male and female children from a family dataset
Date
Sun, 30 May 2010 19:12:48 +0200
<>
Extension of this code to several mothers will require use of -bysort-, btw:
***********
clear*
set obs 6
*Mother's id
gen byte motherid=1
*Birth order
gen byte birth=_n
*child's sex
gen byte chsex=1+inlist(_n,4,5)
label define chsex 1 male 2 female
label values chsex chsex
*born year
gen int bornyear=1982
replace bornyear=1985 in 2
replace bornyear=1986 in 3
replace bornyear=1989 in 4
replace bornyear=1990 in 5
replace bornyear=1995 in 6
*death year
gen deathyear=1994 in 2
expand 2 if !mi(deathyear), gen(myindicator)
gen year=cond(myindicator, deathyear, bornyear)
la def myvals 0 "birth" 1 "death"
la val myindicator myvals
sort year
gen deadmale=sum((chsex==1)*myindicator)
gen deadfemale=sum((chsex==2)*myindicator)
gen alivmale=sum((chsex==1)*!myindicator)-deadmale
gen alivfemale=sum((chsex==2)*!myindicator)-deadfemale
drop if myindicator
drop myindicator year
sort birth
list, noo sepby(chsex)
***********
HTH
Martin
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Anna Reimondos
Sent: Sonntag, 30. Mai 2010 11:44
To: [email protected]
Subject: st: Calculate number of alive male and female children from a
family dataset
Hello statalisters,
I have a dataset which had the complete birth histories of a survey of
women. The data is in 'long' format so that each woman has one
observations for each child she has had. The information I want to
work with is the child's date of birth, date of death (if applicable)
and sex. From this I would like to calculate at the time of each
birth, how many alive male children the woman had, how many alive
female children, how many dead male children and how many dead female
children. That is the part I am having trouble with.
Here is a quick example of one hypothetical womans history:
This woman has had 6 births (4 males, and 2 females - where sex=1 is
male and sex=2 is female)
All the children were still alive at the time of the survey except for
the second birth. That child died in 1994.
Based on this information I want to fill in the variables alivmale
(number of male children alive at the time of the birth of child X),
alivfem, deadmale and deadfem.
For example at the time of the birth of the 6th child the woman had
had 2 male children, 2 female children and the 1 male child that had
died.
motherid Birth sex bornyear deathyear alivmale alivfem
deadmale deadfem
1 1 1 1982 0
0 0 0
1 2 1 1985 1994 1
0 0 0
1 3 1 1986 2
0 0 0
1 4 2 1989 3
0 0 0
1 5 2 1990 3
1 0 0
1 6 1 1995 2
2 1 0
I can't seem to get it right.
I thought it would be a fun problem to get into, but after struggling
now for quite a long time it is not so fun anymore.
I hope someone will be able to see a solution....
Thanks
Anna
P.S I have written the code to produce this dataset in STATA as well
if that helps.
set obs 6
*Mother's id
gen motherid=1
*Birth order
gen birth=.
forvalues n=1/6 {
replace birth=`n' in `n'/`n'
}
*child's sex
gen chsex=1 in 1/3
replace chsex=2 in 4/5
replace chsex=1 in 6/6
label define chsex 1 male 2 female
label values chsex chsex
*born year
gen bornyear=.
replace bornyear=1982 in 1/1
replace bornyear=1985 in 2/2
replace bornyear=1986 in 3/3
replace bornyear=1989 in 4/4
replace bornyear=1990 in 5/5
replace bornyear=1995 in 6/6
*death year
gen deathyear=.
replace deathyear=1994 in 2/2
*
* 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/
*
* 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/