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

# Re: st: RE: Calculate number of alive male and female children from a family dataset

 From Steve Samuels To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Calculate number of alive male and female children from a family dataset Date Mon, 31 May 2010 14:59:53 -0400

```I should add that I started writing my code before I saw Martin's much
more compact version, which I recommend. To get prior totals, modify
his code by adding the statements:

replace alivmale    = alivmale-1       if !myindicator &  chsex==1
replace alivfemale = alivfemale-1    if !myindicator &  chsex==2

Also, in my post,  the first input line " 1 1 1 1982 "  is missing a
"." at the end

Steve

On Mon, May 31, 2010 at 2:39 PM, Steve Samuels <sjsamuels@gmail.com> wrote:
> Anna, In our pregnancy studies, we  calculate events prior to each
> birth.  If that is what you want, the following code should work.
> Note that you will have to  tweak it for multiple births.
>
> Steve
>
>
> *******CODE BEGINS****************
> clear
> input motherid order sex bornyear deathyear
> 1 1 1 1982
> 1 2 1 1985 1994
> 1 3 1 1986 .
> 1 4 2 1989 .
> 1 5 2 1990 .
> 1 6 1 1995 .
> end
> list
> gen m_alive=0
> gen f_alive=0
> gen m_prior=0
> gen f_prior=0
> gen year = bornyear
>
> expand 2 if deathyear !=., gen(status)
> bysort motherid (year): replace year =deathyear if deathyear!=.  & status==1
> ** MALES
> bysort motherid (year): replace m_dead= sum(sex==1 & status==1)
> bysort motherid (year): replace m_prior = sum(sex==1 & status==0)
> replace m_prior = m_prior -1 if status==0 & sex==1
>  replace m_alive = m_prior - m_dead
>
> ** FEMALES
> bysort motherid (year): replace f_dead= sum(sex==2 & status==1)
> bysort motherid (year): replace f_prior = sum(sex==2 & status==0)
> replace f_prior = f_prior -1 if status==0 & sex==2
>  replace f_alive = f_prior - f_dead
> drop if status==1
> ***********CODE ENDS****************************************************
>
>
> On Sun, May 30, 2010 at 1:12 PM, Martin Weiss <martin.weiss1@gmx.de> wrote:
>>
>> <>
>>
>> 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
>>
>> drop if myindicator
>> drop myindicator year
>> sort birth
>>
>>
>> list, noo sepby(chsex)
>> ***********
>>
>>
>> HTH
>> Martin
>>
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu
>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Anna Reimondos
>> Sent: Sonntag, 30. Mai 2010 11:44
>> To: statalist@hsphsun2.harvard.edu
>> 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),
>> 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
>> 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/
>>
>
>
>
> --
> Steven Samuels
> sjsamuels@gmail.com
> 18 Cantine's Island
> Saugerties NY 12477
> USA
> Voice: 845-246-0774
> Fax:    206-202-4783
>

--
Steven Samuels
sjsamuels@gmail.com
18 Cantine's Island
Saugerties NY 12477
USA
Voice: 845-246-0774
Fax:    206-202-4783

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