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

Re: st: foreach nesting


From   khigbee@stata.com
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: foreach nesting
Date   Tue, 16 Sep 2003 07:07:59 -0500

Al <APasaoa@aol.com> 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 <n.j.cox@durham.ac.uk> 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    khigbee@stata.com
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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index