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

From |
"Gabi Huiber" <ghuiber@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: not sure about my loop results |

Date |
Sat, 20 Dec 2008 11:19:34 -0500 |

I'm not sure what your data structure is, but I assume that you have one observation per respondent, respondents can have between 0 and 11 siblings, and in different data sets some of the yearsib variables may be missing. The line gen agesib1=2006-yearsib1 if yearsib1!=. will do two things. First, it will generate the variable agesib1 if the variable yearsib1 exists, and it will produce an error "yearsib1 not found" otherwise, thanks to your inline "if". Second, it will produce missing values if the variable yearsib1 exists, but some of its values are missing. Your inline "if", however, has nothing to do with that. Subtracting a missing from 2006 will produce a missing. That's the default behavior. I don't think that's the job you meant for your "if" to do. Instead, I think that what you're looking for is a way to generate agesib1 if yearsib1 exists, and not generate it otherwise (so skip that case, rather than produce an error message). I would restate your loop as follows: forvalues i=1/`n' { capture confirm variable yearsib`i' if _rc==0 { gen agesib`i'=2006-yearsib`i' } } In other words, check first if yearsib`i' exists; if it does, produce a variable named agesib`i'. If there is no chance that any of your yearsib variables are missing, then the loop above is even simpler, and there's no "if" involved -- inline or otherwise: forvalues i=1/`n' { gen agesib`i'=2006-yearsib`i' } This really is all there is to it. You will get ages of siblings as intended, for all respondents for whom you have siblings with a non-missing year of birth. Gabi On Sat, Dec 20, 2008 at 10:38 AM, Nirina F <fstata@gmail.com> wrote: > Hello, > > 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/

**Follow-Ups**:**Re: st: not sure about my loop results***From:*"Nirina F" <fstata@gmail.com>

**References**:**st: not sure about my loop results***From:*"Nirina F" <fstata@gmail.com>

- Prev by Date:
**st: not sure about my loop results** - Next by Date:
**Re: st: not sure about my loop results** - Previous by thread:
**st: not sure about my loop results** - Next by thread:
**Re: st: not sure about my loop results** - Index(es):

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