Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Create Variable containing Generations per Household


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Create Variable containing Generations per Household
Date   Wed, 19 Sep 2012 16:43:15 +0200

On Wed, Sep 19, 2012 at 4:11 PM, Laura Marlene Große  wrote:
> One of my criteria to draw a random subsample for my future research will be
> how many generations do live in one household. Using my variable "relation
> to the head of the household" i have identified the different types of
> relatives and their inter-generational position.<snip>
> In my individual based dataset (individual and household ID's), I would like
> to create a new variable which contains the number of generations per
> household. <snip> I assume that I will have to write a loop using foreach or similar.

There is not need to loop, see the example below:

*------------------------- begin example -------------------------
// create some example data
clear
input fam_id person_id rel_head
1 1 1
1 2 2
1 3 3
2 1 1
2 2 2
2 3 6
2 4 4
2 5 5
2 6 17
3 1 1
3 2 2
end

// create generations
gen generation =                                               ///
    cond(inlist(rel_head, 6, 7, 12), 1,                        ///
    cond(inlist(rel_head, 1,2,8,14,21,23), 2,                  ///
    cond(inlist(rel_head, 3, 4, 9, 18, 19, 20, 22, 25, 13), 3, ///
    cond(inlist(rel_head, 5, 15, 16, 24), 4,                   ///
    cond(inlist(rel_head, 17), 5, .)))))

// count the number of generations per family	
bys fam_id generation : gen byte first = _n == 1 if generation < .
bys fam_id : egen n_gen = total(first)

// see the result
list, sepby(fam_id)
	
// variable first no longer needed
drop first
*-------------------------- end example --------------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index