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

From |
siyama@who.int |

To |
statalist@hsphsun2.harvard.edu |

Subject |
st: RE: RE: Re: Two "forvalues" loops working together |

Date |
Fri, 14 May 2004 13:10:34 +0200 |

Many thanks to Jean Ries for pointing-out the use of a macro and Nick Cox for perfecting the necessary code. I have got exactly what I need. Viva "stata-list" Amani -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: 14 May 2004 12:19 To: statalist@hsphsun2.harvard.edu Subject: st: RE: Re: Two "forvalues" loops working together This won't work yet, as constructs like `Y+10' won't work the way you want them to work. There is, as far as I know, no undocumented syntax for looping through two sets of values simultaneously. You can go `=`Y'+10' to get an evaluation on the fly. You can also go `X--' to get decrementing on the fly. But in this case, the problem is not a full-blown multiple loop problem: it can be reduced to a single loop, as Jean showed. There is, however, a trade-off between concision and clarity. In this case I would be driven mostly by the consideration that I might well want to come back to this code and understand it immediately and be able to modify it quickly. Clarity would like win over concision in favour of something like local X = 43 forvalues Y = 120(10)530 { local Y2 = `Y' + 10 predict prob`Y', pr0(`Y', `Y2') predict exp`Y', e0(`Y', `Y2') local where = `samp' - `X' local X = `X' - 1 replace prob = prob`Y' in `where' replace exp = exp`Y' in `where' } You can squeeze this to fewer lines with local X = 43 forvalues Y = 120(10)530 { predict prob`Y', pr0(`Y', `=`Y' + 10') predict exp`Y', e0(`Y', `=`Y' + 10') local where = `samp' - `X--' replace prob = prob`Y' in `where' replace exp = exp`Y' in `where' } and even fewer local X = 43 forvalues Y = 120(10)530 { predict prob`Y', pr0(`Y', `=`Y' + 10') predict exp`Y', e0(`Y', `=`Y' + 10') replace prob = prob`Y' in `=`samp' - `X'' replace exp = exp`Y' in `=`samp' - `X--'' } but I think many Stata users would find this more difficult to read. There is a much more detailed discussion of such parallel loops problems in Stata Journal 3(2):185-202 (2003). Nick n.j.cox@durham.ac.uk jean ries > Here is a suggestion. Hope it helps. > > local X 43 > > forvalues Y = 120(10)530 { > predict prob`Y', pr0(`Y',`Y+10') > predict exp`Y', e0(`Y',`Y+10') > replace prob=prob`Y' in `samp-`X'' > replace exp=exp`Y' in `samp-`X'' > loc X = `X' - 1 > } Amani Siyam > I wish to make two "forvalues" loops work together rather than nested > as shown by my suggested code below. Is there a way I can do that in > STATA > > > local samp=_N+43 > set obs `samp' > foreach M of newlist prob exp { > gen `M'=. > } > intreg ptiming timing if ~npointer1, noskip intreg forvalues Y = > 120(10)530 ; X = 43(-1)1 { > predict prob`Y', pr0(`Y',`Y+10') > predict exp`Y', e0(`Y',`Y+10') > replace prob=prob`Y' in `samp-`X'' > replace exp=exp`Y' in `samp-`X'' > } > local samp=_N-43 > slist prob exp in `samp'/l > * * 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/ * * 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:
**st: PANTEST2** - Next by Date:
**st: Creating panel data** - Previous by thread:
**st: PANTEST2** - Next by thread:
**st: Creating panel data** - Index(es):

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