In trying to find out precisely what was causing the problem, I made
some vary simple test programs that sort of mimic the structure of my
original program: there is -run_test.do-, which calls the other
programs, -test_loop.do-, which just does a simple for-loop, and
-run_program.ado-, which simply tries to run/do/include -test_loop.do-
(see complete code below). Running -run_test.do- causes error r(9611)
just like my original program, but it does not have the problem with
modifying the loop variable in the loop (which is a problem for other
reasons anyway, though, so thanks for catching it).
What I have concluded from my tests is that the error is thrown when
an ADO-file -INCLUDE-s a do-file with a loop (any kind, foreach,
forvalues, or while). The error is thrown at the moment the loop in
the DO-file starts. Note that including the loop itself in the
ADO-file doesn't cause an error, nor does -run-ing or -do-ing the
DO-file with the loop. Indeed, I have developed a work-around for my
original problem by simply copying the code I was trying to -include-
and pasting it in the ado-file itself; so the code in question works
when literally in the ado-file but not when it is in a do-file that is
-include-d in the ado-file.
It seemed fishy how capricious the error was to me, so I tried it on
another Windows machine and on a UNIX machine using Stata IC, both of
which also threw the error.
Thanks for everyone's help again.
CODE:
*******************************
* run_test.do
* This is the do-file that should actually be executed; it will call
everything else
* Including the loop do-file in a do-file doesn't cause an error
include "test_loop.do"
* Run a custom ado file -- note that the error is thrown without the
second display line (returned...etc.) being displayed
noi: di "About to run program"
run_program
noi: di "Returned from program"
* END OF RUN_TEST.DO
*******************************
* test_loop.do
* This do-file just does a simple loop (this is a forvalues loop, but
I've tried it with while and foreach loops and found the same problem)
* This program runs a loop to see if it will work
noi: di "Starting test loop..."
forvalues x=1/10 {
di "x: `x'"
}
noi: di "Exiting test loop..."
* END OF TEST_LOOP.do
*****************************
* run_program.ado
* This is an ado-file that, first, executes a loop with no problem,
* Then, runs/does test_loop.do, again with no problem
* Then, tries to -include- test_loop.do, which throws an error
program define run_program
noi: di "{res}In run_program"
* Running a regular loop in the ado-file poses no problems
forvalues x=1/10 {
noi: di "{txt}in run_program.ado: `i' x: `x'"
}
* And, I can -run- or -do- the do-file with no problem either
noi: di "{txt}running test_loop"
run "test_loop.do"
noi: di "{txt}test_loop ran successfully"
noi: di "{txt}doing test_loop"
do "test_loop.do"
noi: di "{txt}did test_loop successfully
* But -INCLUDE-ing it causes error r(9611)
noi: di "{txt}About to -include- test_loop"
include "test_loop.do"
*END OF RUN_PROGRAM:
end
On Fri, Oct 17, 2008 at 9:23 AM, William Gould, StataCorp LP
<[email protected]> wrote:
> Brian Karfunkel <[email protected]> reports, "I'm getting a weird error,
> -r(9611)-, when I run my program." Brian quoted about 50 lines of code.
>
> I beleive I have uncovered the problem. In Code Block 1, Brian has
> the following
>
> foreach var of varlist `model_varlist' {
> ...
> if (...) {
> local model_varlist "`model_varlist' ..."
> }
> }
>
> To wit, Brian changes the contents of the macro controlling the
> loop, `model_varlist', in the body of the loop.
>
> That is not allowed. It is not allowed in any of the syntactical
> variants of -foreach-, including -foreach ... of varlist-,
> -foreach ... of local-, ..., or -foreach ... in-. It is not allowed
> for speed-of-execution reasons.
>
> One way to code what Michael wants to do is
>
> local i = 1
> local var : word `i' of `model_varlist'
> while ("`var'"!="") {
> ...
> if (...) {
> local model_varlist "`model_varlist' ..."
> }
> local var : word `++i' of `model_varlist'
> }
>
> By the way, another way of coding
>
> local var : word `++i' of `model_varlist'
>
> is
>
> local i = `i' + 1
> local var : word `i' of `model_varlist'
>
>
> -- Bill
> [email protected]
> *
> * 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/
>
--
Brian Karfunkel
Research Fellow
Stanford Law School
Crown Quadrangle
559 Nathan Abbott Way
Stanford, California 94305
+1.650.721.5694 (office) | +1.978.886.1607 (mobile)
[email protected]
*
* 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/