Bookmark and Share

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]

Re: st: Problems with expand og reverting to original dataset


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Problems with expand og reverting to original dataset
Date   Thu, 20 Jan 2011 13:18:02 +0000

Without trying to follow or re-create your code, it seems that a
family here means precisely the same mother and the same father. (Real
life naturally can be much more complicated.)

So, the age difference between siblings is just

bysort mother_id father_id (birth_date) : gen diff = birth_date[2] -
birth_date[1]

Here -diff- is guaranteed to have the same value for both siblings and
to be 0 or positive (assuming no missings). The key Stata trick is
that subscripts under the aegis of -by:- are interpreted within
groups. You are using that principle but this code uses it more
simply.

So your criterion is then simply

keep if diff < whatever

I can't see any need whatsoever for -expand- as the age differences
can be calculated from the dataset as is.

Nick

On Thu, Jan 20, 2011 at 1:05 PM, Grethe Søndergaard
<[email protected]> wrote:

> My dataset consists of siblings (two siblings from each family).
>
> person_id1
> mother_id
> father_id
> sibling_id
> birth date
>
>
> I only want to keep siblings in my dataset with a certain age
> difference, lets say 5 years or less.
> I have tried to do this using expand but when I am not sure how to
> revert to my original dataset. This is what I have done:
>
> ** duplicating each observation [no] times
> expand no, gen(dub)
>
> ** creating an (arbitary) id to keep track of the duplicates
> sort mother_id father_id person_id1
> by mother_id father_id person_id1: gen copyid = _n
>
> ** copying
> sort mother_id father_id
> by mother_id father_id: gen person_id2 = person_id1[no*copyid]
>
> ** adding age difference variable
> by mother_id father_id: gen birthdate_2  = birthdate_1[no*copyid]
>
> ** deleting redundant copies
> drop if person_id1 >= person_id2
>
> * creating age differences
> gen agedif = birthdate_1 - birthdate_2
> replace agedif=agedif *-1 if agedif<0
> keep if agedif<6
>
> So far so good. My dataset now consits of pairs of siblings with an
> age difference of less than 6 years. There are no dublicates. But how
> do I revert to my original dataset??
> (I have tried to keep if dub==0 but that deleted a lot of observations
> - and that shouldn't happen since there were no dublicates)
>

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index