Laura Gibbons wrote:
I'm writing a simulation program where if something happens, I want to
jump out of a set of nested loops and go to the next replication. Is
there a short cut for putting -continue, break- in every nested loop?
When I learned Fortran with numbered program lines (cards, actually, this
was 36 years ago!), I could just write "GO TO 100" which meant go to line
100. Is there anything similar in Stata?
--------------------------------------------------------------------------------
You can place the nested loops into a subprogram that is called by your
simulation program. Note that, if you use -forvalues- or -foreach- (as
opposed to -while-) for the loops in the satellite program, then you must
throw an exception in order to terminate early: see
www.stata.com/statalist/archive/2006-10/msg01186.html .
You might be able to use -while- loops that test two conditions (loop
counter & breakout-flag status) in your main simulation program and avoid
using a subprogram altogether. -while- might be a little slow for use in
nested loops inside a program called by -simulate-, however. My
understanding is that -forvalues- and -foreach- are faster.
If you can invest the time up-front, Mata is an alternative that might be
fastest in execution. And you can code -if (something == happens) goto
L100- in it, although another style might prove better.
Joseph Coveney
program define mysimulation, rclass
version 9.2
.
.
.
tempname A
local count . . .
matrix define `A' = . . .
.
.
.
capture mynesteddoloops , inputarg(`count') ///
resultarg(`A')
if (_rc == -1) {
.
.
.
}
else if (_rc == 0) {
.
.
.
}
else exit = _rc
.
.
.
end
*
program define mynesteddoloops
version 9.2
syntax , inputarg(integer) resultarg(name)
.
.
.
forvalues i = 2/`inputarg' {
forvalues j = 1/`=`i'-1' {
if (. . .) exit = -1
matrix define `resultarg'[`i',`j'] = . . .
.
.
.
}
}
end
*
simulate . . . : mysimulation . . .
exit
*
* 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/