Statalist


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

Re: st: RE: Using Mata within a foreach loop = r(1)


From   Pablo Mitnik <[email protected]>
To   [email protected]
Subject   Re: st: RE: Using Mata within a foreach loop = r(1)
Date   Fri, 07 Dec 2007 11:10:05 -0600

Thanks much for your answer -- this greatly clarifies things.
This way foreach treats end statements seems rather undesirable, doesn't it? And, yes, at least it should be in the help somewhere.
Pablo


Austin Nichols wrote:

Pablo Mitnik <[email protected]>:
You are not back in Stata; you never entered Mata. The -foreach- loop
aggregates all the commands before running, one of which is -end-,
which exits the loop and executes none of it.

Try

foreach i in 1 2 {
di `i'
end
}

to see what I mean. A similar problem arises is you try to -input-
data inside a -program- etc. The use of -end- to end many commands is
somewhat problematic in this case. I agree that -help end- should
explain this.

On 12/7/07, Pablo Mitnik <[email protected]> wrote:

Nick Cox wrote:

The answer to your 2 is Yes, with some exceptions for good reasons, as
when you
call Stata from Mata, but that is not the issue as I understand it.

The issue is that at that point in your code you are back in Stata and
so
(in context) -end- is illegal.

Why am I back in Stata at this point?



There can be no legal or meaningful -end- of a state without some
beginning
that defines the start of that state (cf. much philosophy and all
eschatology).

Less enigmatically, in Stata -end- on a line by itself can indicate the
end of
a program which started with a -program- statement, and in Mata -end-
indicates
a desire to return to Stata. -help end- returns nothing, because -end-
is
not a statement by itself. But -search end- brings up some pointers,
some
of which are relevant to your 1.

It's a hard lesson that Stata's not behaving as you would guess, imagine
or prefer
does not in itself constitute a bug.

Pablo Mitnik

Thanks much for the suggestion . I can do that, and will probably do it.
But:
1. Is there a documented "end" command in Stata? Typing "help end"
returns nothing, while "search end" only returns the Mata end statement.
2. If I am "within Mata," shouldn't everything be run in Mata?

Maarten buis wrote:


--- Pablo Mitnik <[email protected]> wrote:



This is rather inconvenient when you have a longer code to run in
Mata; and I haven't found any explanation in the manuals why the code


with "mata" and "end" shouldn't work -- my bet is that this is a bug.


The end command is run in Stata not Mata, so you get the correct error
message.

The way to do this in a more convenient way is to first create a mata
function, so within the loop all you need is a single call to Mata.
This is explained in terms of .ado programming in -help m1_ado-.

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

--
Pablo A. Mitnik
University of Wisconsin-Madison (http://www.wisc.edu/ )
Department of Sociology ( http://www.ssc.wisc.edu/soc/)
Center on Wisconsin Strategy (http://www.cows.org/ )
1180 Observatory Drive
Room 7114A
Madison, WI 53706
TEL (608) 2621839
E-mail: [email protected]


*
* 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/
--
Pablo A. Mitnik
University of Wisconsin-Madison (http://www.wisc.edu/ )
Department of Sociology ( http://www.ssc.wisc.edu/soc/)
Center on Wisconsin Strategy (http://www.cows.org/ )
1180 Observatory Drive
Room 7114A
Madison, WI 53706
TEL (608) 2621839
E-mail: [email protected]

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