Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I do a single-line if statement in Mata?

    I have this code in a Stata 13.1 do file:

    Code:
    cls
    mata:
        s = 1
        t = 1
        if (s == t) display("equal")
    end
    This displays an r(3000) error:

    Code:
    unexpected end of line
    (0 lines skipped)
    This code appears to conform to the first case in -help m2_if-. However, I've seen posts elsewhere that say Mata is actually expecting an -else- statement here, because if you type something like

    Code:
    if (1 == 1) display("equal")
    in interactive mode, it displays an > and prompts you to continue entering the command i.e. an else block. But, adding a semicolon to the end of the line doesn't fix this, i.e. this code still throws the same error:

    Code:
    cls
    mata:
        s = 1
        t = 1
        if (s == t) display("equal");
    end
    If I make this into a multi-line if statement:

    Code:
    cls
    mata:
        s = 1
        t = 1
        if (s == t) {
            display("equal")
        }
    end
    I get the same error. I need to add a semicolon after the closing brace to get this to work, like this:

    Code:
    cls
    mata:
        s = 1
        t = 1
        if (s == t) {
            display("equal")
        };
    end
    I don't see anything in -help m2_semicolons- that describes this behaviour either. What am I missing?
    Last edited by Michael Anbar; 24 Sep 2014, 16:57.

  • #2
    Interesting question. My hunch is that the command looks ahead and cannot find the end. This old post explains the logic of -mata if- and the use of semi-colons.

    Adding another statement in the next line solves the problem. For example,

    Code:
    mata:
        s = 1
        t = 1
        if (s == t) display("equal")
        display("whatever")
    end
    Or substitute the last statement with a semicolon would also work.

    Comment


    • #3
      I came across that old post, and that's what gave me the idea that Mata might be missing the else clause (specifically, the so-called optional else clause). You're correct that this code works:

      Code:
      mata:
          s = 1
          t = 1
          if (s == t) display("equal")
          ;
      end
      but as I found in my original post, this code fails:

      Code:
      mata:
          s = 1
          t = 1
          if (s == t) display("equal");
      end
      I guess the first code sample is my only option in this case, because I don't always want to put a line of dummy code immediately after my if statements, just so Mata's poorly designed parser won't throw errors, either in the one-line case or in the case of an actual -if- block.

      Comment


      • #4
        You can do this, too:
        Code:
        mata:
            s = 1
            t = 1
            if (s == t) display("equal");;
        end
        It's explained further by Bill Gould in an old Statalist post here. Mata's design tries to accommodate two very different circumstances of usage (interactive and noninteractive), having mutually incompatible requirements. Given these software design requirements, I hesitate to declare Mata's parser to be so poorly designed.

        Comment


        • #5
          That discrepancy makes more sense now; thanks for linking me to that post (I hadn't come across that earlier). The reason I question the design of Mata's parser is because I'm accustomed to more modern languages/environments that seem to handle the conflict between programmatic use and the mechanics of read-eval-print loops without this inconsistency. I understand that this is not the purpose of Stata nor one of its strengths, but it does make for frustration when learning Mata. I guess it's a matter of fully coming to grips with -help m2_syntax- before writing code.

          Comment

          Working...
          X