Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: foreach nesting


From   [email protected]
To   [email protected]
Subject   Re: st: foreach nesting
Date   Tue, 16 Sep 2003 07:07:59 -0500

Al <[email protected]> asks:

> Can the -foreach- command be nested?  Stata crashes if I try to put a
> foreach inside another foreach.
> 
> for example:
> 
> local N "0 5 10";
> local Y "y1 y2 y3";
> foreach n in `N'
>    {foreach y in `Y'
>       {reg `y' X if t>`n' } };

And Nick Cox <[email protected]> has answered showing how it
should be done.

I would just like to clarify that in Stata 8 you get an error
message, not a crash.

    . do examp

    . #delimit ;
    delimiter now ;
    . local N "0 5 10";

    . local Y "y1 y2 y3";

    . foreach n in `N'
    >    {foreach y in `Y'
    >       {reg `y' X if t>`n' } };
    program error:  code follows on the same line as open brace
    r(198);

    end of do-file
    r(198);


And, the error message gives a clue as to the problem.

One other thing to be aware of using the semicolon as the delimiter,
is to remember to put one after the curly braces.

    foreach n in `N' { ;
            foreach y in `Y' { ;
                    reg `y' X if t > `n' ;
            } ;
    } ;

otherwise it is as if they were on the same line as whatever follows.


Ken Higbee    [email protected]
StataCorp     1-800-STATAPC

*
*   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/



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