[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Stephen P. Jenkins" <stephenj@essex.ac.uk> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
st: -exit- within -forvalues- loop |

Date |
Tue, 22 Nov 2005 15:17:21 +0000 |

I am checking a condition within a -forvalues- loop, with the idea that if the condition fails, the program will be stopped using -exit-. I found some unexpected behaviour with one set of code ("version 1" below). "Version 2" produced the expected behaviour. Note that version 1 code gets through ok to the "Hello World" display command (which I hadn't expected), whereas the version 2 code stops before that display command. Advice please concerning why "version 1" works as it does, together with any related tips on the code. I note from online help for -exit- that "More generally, exit causes Stata to terminate the current process and return control to the calling process." Is it that the return in the version 1 case was to one level higher, i.e. outside the loop? Or something like that? (I don't have access to manuals at present.) thanks Stephen Here follows code and then output from using it. (Same results in version 8.2 and version 9.1) ++++++++++++++++++++++++++++++++++++++++++++++++ // D is exact multiple of both primes local M = 2 tempname primes mat `primes' = (3, 5) local D = 15 ***** version 1 *********** forval i = 1/`M' { local prime = `primes'[1,`i'] if mod(`D',`prime') == 0 { di as err "{p 0 0 4}Number of draws should not " di as err "be exact multiple of chosen prime. " di as err "(Problem is for equation `i' " di as err "and prime `prime'.){p_end}" exit } } di "hello world #1" ***** version 2 *********** forval i = 1/`M' { local prime = `primes'[1,`i'] if mod(`D',`prime') == 0 { di as err "{p 0 0 4}Number of draws should not " di as err "be exact multiple of chosen prime. " di as err "(Problem is for equation `i' " di as err "and prime `prime'.){p_end}" local y = 1 } } if `y' == 1 exit di "hello world #2" +++++++++ output +++++++++++++++++++++++++++++++++++++++++++++++++++ . // D is exact multiple of both primes . . local M = 2 . . tempname primes . mat `primes' = (3, 5) . . local D = 15 . . ***** version 1 *********** . . forval i = 1/`M' { 2. local prime = `primes'[1,`i'] 3. if mod(`D',`prime') == 0 { 4. di as err "{p 0 0 4}Number of draws should not " 5. di as err "be exact multiple of chosen prime. " 6. di as err "(Problem is for equation `i' " 7. di as err "and prime `prime'.){p_end}" 8. exit 9. } 10. } Number of draws should not be exact multiple of chosen prime. (Problem is for equation 1 and prime 3.) Number of draws should not be exact multiple of chosen prime. (Problem is for equation 2 and prime 5.) . di "hello world #1" hello world #1 . . . . ***** version 2 *********** . forval i = 1/`M' { 2. local prime = `primes'[1,`i'] 3. if mod(`D',`prime') == 0 { 4. di as err "{p 0 0 4}Number of draws should not " 5. di as err "be exact multiple of chosen prime. " 6. di as err "(Problem is for equation `i' " 7. di as err "and prime `prime'.){p_end}" 8. local y = 1 9. } 10. } Number of draws should not be exact multiple of chosen prime. (Problem is for equation 1 and prime 3.) Number of draws should not be exact multiple of chosen prime. (Problem is for equation 2 and prime 5.) . if `y' == 1 exit end of do-file . -- Professor Stephen P. Jenkins <stephenj@essex.ac.uk> Institute for Social and Economic Research (ISER) University of Essex, Colchester, CO4 3SQ, UK Tel: +44 (0)1206 873374. Fax: +44 (0)1206 873151. http://www.iser.essex.ac.uk Survival Analysis using Stata: http://www.iser.essex.ac.uk/teaching/degree/stephenj/ec968/ ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program. * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

- Prev by Date:
**Re: st: RE: Timevarying IDs in a panel dataset** - Next by Date:
**RE: st: RE: Timevarying IDs in a panel dataset** - Previous by thread:
**st: New version of -smileplot- on SSC** - Next by thread:
**Re: st: -exit- within -forvalues- loop** - Index(es):

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