Statalist The Stata Listserver


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

Re: st: If in Mata


From   [email protected] (William Gould, Stata)
To   [email protected]
Subject   Re: st: If in Mata
Date   Wed, 12 Apr 2006 08:58:40 -0500

Daniel Schneider <[email protected]> writes, 

> I am lost, and I do not know anymore what I am doing wrong with this
> very simple problem. I have been trying to get a very simple if-clause
> to run in Mata, but it hasn't worked at all. I have finally reduced the
> if-statement to the most simple form:
> 
>        . mata: if (1==1) results[1,1]
>
> What I get is 
> 
>          unexpected end of line
>          <istmt> incomplete

I appreciate Daniel's confusion, especially since he is not doing anything 
wrong.  So I'm going to give a rather long-winded explanation as to why 
this happens and how Daniel can work around it.

Mata is a compiler, not an interpreter, and the full syntax of the 
-if- statement is 

          if (<exp>) <stmt> 
          else <stmt>

of which the short form is

          if (<exp>) <stmt> 

When the Mata compiler sees -if-, it looks ahead for the optional -else- 
clause.  That's the source of David's problem.  

Try the following experiment

        . mata

        : result = 5

        : if (1==1) result
        >
        >
        > 

Obviously -if (1==1)- is true, but rather than listing result, Mata, in
interactive mode, keeps issuing a ">" prompt.  ">" means "continue the
incomplete line, please".

Why does Mata do this?  Mata needs to see what follows the -if= becaue it
might be an -else-.

This looking-ahead problem does not arise when we enter Mata programs because,
as we enter the lines, we are unaware of where the Mata compiler is exactly
looking.  In interactive mode, however, we are very mcuh aware that 
Mata is behind us.  We ahve typed, 

        : if (1==1) result 
        >
        >

and we cannot escape.

There are two choices other than hitting break:  We must enter another
statement so that the compiler will be satisified that what follows is not an
-else-, or we must somehow tell the compiler "Stop, there is no more".

Semicolon tells Mata that there is no more, 

        > ;
        5

        : _

In total, we typed, 

        : if (1==1) result 
        >
        >
        > ;
        5

but we could have typd

        : if (1==1) result ;
        5

By the way, just to show you that typing another statement would also work, 

        : if (1==1) result 
        >
        >
        > "display this string"
        5 
        display this string

Let me review:

    1.  This "problem" occurs only with -if- statements.

    2.  It arises because -if- has an optional -else-, and so the compiler 
        must look ahead to know what is coming.

Most users will never run into this because no one ever wants to type an -if-
statement interactively anyway.  

If David is bound and determined to code Mata's -if- interactively, and 
from Stata (say in an ado-file), he is one solution:

        
        mata {
                if (1==1) results[1,1]
        }

-- Bill
[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