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

From |
"Nirina F" <fstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Sat, 20 Dec 2008 11:45:41 -0500 |

Thank you very much Gabi for responding, I tried what you suggested and I get the same results as I had which are agesib1 till agesib6. Also I tried to do it by number of brothers and sisters and I know something is wierd because I get agebrother1 till agebrother5 then agesiter1 only. This tells me that may be the loop is doing just for one type of respondent having 5 brothers and 1 sister which means 6 siblings in total. However this is the tabulate from brother and sister. . tab sibnum number of | siblings, | not | including | resp | Freq. Percent Cum. ------------+----------------------------------- no siblings | 72 1.61 1.61 1 | 200 4.48 6.09 2 | 434 9.72 15.82 3 | 662 14.83 30.65 4 | 727 16.29 46.93 5 | 726 16.26 63.19 6 | 726 16.26 79.46 7 | 428 9.59 89.05 8 | 263 5.89 94.94 9 | 131 2.93 97.87 10 | 51 1.14 99.01 11 | 44 0.99 100.00 ------------+----------------------------------- Total | 4,464 100.00 . tab brother see notes | Freq. Percent Cum. ------------+----------------------------------- 0 | 468 10.48 10.48 1 | 1,003 22.47 32.95 2 | 1,169 26.19 59.14 3 | 835 18.71 77.84 4 | 570 12.77 90.61 5 | 275 6.16 96.77 6 | 93 2.08 98.86 7 | 40 0.90 99.75 8 | 8 0.18 99.93 9 | 3 0.07 100.00 ------------+----------------------------------- Total | 4,464 100.00 . tab sister see notes | Freq. Percent Cum. ------------+----------------------------------- 0 | 471 10.55 10.55 1 | 943 21.12 31.68 2 | 1,153 25.83 57.50 3 | 838 18.77 76.28 4 | 614 13.75 90.03 5 | 293 6.56 96.59 6 | 97 2.17 98.77 7 | 40 0.90 99.66 8 | 9 0.20 99.87 9 | 6 0.13 100.00 ------------+----------------------------------- Total | 4,464 100.00 . Nirina On Sat, Dec 20, 2008 at 11:19 AM, Gabi Huiber <ghuiber@gmail.com> wrote: > 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/ > * * 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/

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

**Re: st: not sure about my loop results***From:*"Gabi Huiber" <ghuiber@gmail.com>

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

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