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: Generating dummy variable with information of household survey from different observations


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Generating dummy variable with information of household survey from different observations
Date   Mon, 7 May 2012 09:52:36 +0100

Another way to do it:

gen father = 0
gen work = 0
su individual_id, meanonly
qui forval i = 1/`r(max)' {
         replace work = father_row == `i'
         bysort household_id (work) :  replace father = 1 if
individual_id == `i' & work[_N] == 1
}
drop work
l

On Mon, May 7, 2012 at 8:57 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> For a lengthy discussion of such problems, see
>
> FAQ     . . Creating variables recording prop. of the other members of a group
>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>        4/05    How do I create variables summarizing for each
>                individual properties of the other members of a
>                group?
>                http://www.stata.com/support/faqs/data/members.html
>
> and also
>
> FAQ     . . Creating variables recording whether any or all possess some char.
>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>        2/03    How do I create a variable recording whether any
>                members of a group (or all members of a group)
>                possess some characteristic?
>                http://www.stata.com/support/faqs/data/anyall.html
>
> You need to loop over all the individuals in each family. That will be
> a loop over all the individuals, but can be specified in terms of
> -individual_id-
>
> clear
> inp household_id   individual_id   father_row
>  1011     1    .
>  1011     2    .
>  1011     3    1
>  1011     4    1
>
>  1012     1     2
>  1012     2     .
>
>  1013      1    .
>  1013      2    .
>  1013      3    2
>  1013      4    1
>  1013      5    1
>  end
>
> gen father = 0
> su individual_id, meanonly
> qui forval i = 1/`r(max)' {
>        egen work = max(father_row == `i'), by(household_id)
>        replace father = 1 if individual_id == `i' & work == 1
>        drop work
> }
> l
>
> Nick
>
> On Mon, May 7, 2012 at 7:13 AM, Sumiko Hayasaka <sumi.hayasaka@gmail.com> wrote:
>> Thanks Eric!
>>
>> Everything works out until I get to the "foreach" command. It says the
>> expression is too long [r(130)]. What should I do?
>
>  On Sun, May 6, 2012 at 11:44 PM, Eric Booth <eric.a.booth@gmail.com> wrote:
>
>>> clear
>>> inp household_id   individual_id   father_row
>>>  1011     1    .
>>>  1011     2    .
>>>  1011     3    1
>>>  1011     4    1
>>>
>>>  1012     1     2
>>>  1012     2     .
>>>
>>>  1013      1    .
>>>  1013      2    .
>>>  1013      3    2
>>>  1013      4    1
>>>  1013      5    1
>>>  end
>>>
>>>
>>> levelsof individual_id, loc(a)
>>> reshape wide father_row, i(household_id) j(individual_id)
>>> ds father_row*
>>> loc checklist `r(varlist)'
>>> loc checklist:subinstr loc checklist " " ", " , all
>>> foreach n in `a' {
>>>         g father`n' = cond(inlist(`n', `checklist'), 1, 0, .)
>>>        }
>>> reshape long father_row father, i(household_id) j(individual_id)
>>>
>
> On May 6, 2012, at 10:34 PM, Sumiko Hayasaka wrote:
>
>>>> I am trying to generate a dummy variable, with information from a
>>>> household survey, which can tell if a member of the household is a
>>>> father or not. I have a household id, an individual id (per
>>>> household), and a variable that tells me which individual id is marked
>>>> as being a father (members of the family are asked if their father
>>>> lives in the household and to give their father's individual id).
>>>> Therefore, I need to assign a 1 at the row in which someone at the
>>>> household said that was a father. To illustrate this, the data is
>>>> something like this (I am trying to get the "father" variable):
>>>>
>>>> household_id   individual_id   father_row   father
>>>> ------------------------------------------------------------------------
>>>>  1011                 1                      .                1
>>>>  1011                 2                      .                0
>>>>  1011                 3                      1               0
>>>>  1011                 4                      1               0
>>>>
>>>>  1012                 1                       2               0
>>>>  1012                 2                       .                1
>>>>
>>>>  1013                  1                      .                1
>>>>  1013                  2                      .                1
>>>>  1013                  3                      2               0
>>>>  1013                  4                      1               0
>>>>  1013                  5                      1               0
>>>>
>>>>
>>>> So, for example, members number 3 and 4 of household number 1011
>>>> stated that their father is individual number 1 in that household.
>>>> This means that I have to put the 1 of "father" (meaning the household
>>>> member is a father) at the row where father_row indicates (no matter
>>>> how many times this is done).
>>>>

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