Statalist


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

Re: st: not sure about my loop results


From   Michael Foster <mike4kids@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: not sure about my loop results
Date   Sat, 20 Dec 2008 11:52:42 -0500

good morning, 

at least without seeing the data, i can speculate that this seems rather 
complicated to me.

if you have data like these

family   sib    age
1          1       4
1          2      7
2          1      8
2          2      9

in that case , you could do the following
bys family (age): gen sibage=age[2] if _n==1
bys family (age): gen sibage=age[1] if _n==2

you could collapse this down to one line
bys family (age): gen sibage=(age[2]*( _n==1)+age[1]*( _n==2))


if you had more than two sibs per family, i would probably work with 
-reshape-
(especially if you had varying numbers of kids in the family)

is this not what you wanted to do?

/michael


> I was writing the following loop to get the age of each of the
> respondent's sibling.
>
> local n=sibnum
> forvalues i=1(1)`n'{
> gen agesib`i'= 2006-yearsib`i' if yearsib`i' !=.
> }
>
> yearsib is the year of birth of the sibling and 2006 is the year of the survey.
>
> if I -tabulate- sibnum, it varies from 0 to 11 at various frequencies.
>
> However the results of the loop above give me:
>
> agesib1, agesib2, ....till agesib5 only not until agesib11 which is
> wierd to me.
>
> I am not sure if I am getting this agesib for each respondent or just
> the first respondent.
> I have the unique individial identifier and other variables.
>
> Could you please let me know if there is something wrong and how to correct it?
>
> thank you very much,
> Nirina
> *
> *   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index