Announcement

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

  • r(1) error message even though I don't press q

    I use stata version 14.2 and run this code in a do-file (normally there is more stuff in the mata-section, but I have taken it out to reduce the code posted here)



    tokenize "atlantic BC ON prairies QC"
    local j = 0
    foreach i of num 1 2 3 4 5 {
    local j = `j'+1
    // omit North (lack of observations) so adjust local index
    if (`i' == 3) {
    local j = `j'+1
    }
    pause

    mata:
    handle_country = 1
    end
    }



    So, when I run it, I get back:

    . tokenize "atlantic BC ON prairies QC"

    . local j = 0

    . foreach i of num 1 2 3 4 5 {
    2. local j = `j'+1
    3. // omit North (lack of observations) so adjust local index
    . if (`i' == 3) {
    4. local j = `j'+1
    5. }
    6. pause
    7.
    . mata:
    8. handle_country = 1
    9. end
    --Break--
    r(1);


    r(1) says :error . . . . . . . . . . . . . . . . . . . . . . . . . Return code 1
    You pressed Break. This is not considered an error.


    BUT - I did not press Break.

    What is going on???

  • #2
    If you want
    • a block of Mata code terminated by end
    to run within
    • a Stata block surrounded by braces {} - typically a program, but in fact within any brace-enclosed block of code
    you need to
    • define your Mata code as a Mata function outside the brace-enclosed block
    • call the Mata function within the brace-enclosed block
    Even though your "end" is meant to terminate a Mata block, at the time Stata is parsing the loop, the fact that the "end" belongs to Mata and not to Stata is not recognized, and causes Stata to react as it does.

    Consider the following illustration.
    Code:
    forvalues i = 1/2 {
        display "i is `i'"
        end
    }
    Code:
    . forvalues i = 1/2 {
      2.         display "i is `i'"
      3.         end
    --Break--
    r(1);
    
    end of do-file
    
    --Break--
    r(1);
    
    .
    I have seen this explained better elsewhere on Statalist, and a well-constructed search might find it, but for now, this is the best I can do. It "doesn't make sense" but that's the way Stata is.

    Code:
    clear all
    
    mata: 
    function foo () {
        gnxl = 42
        gnxl
    }
    end
    
    local j = 0
    foreach i of num 1 2 3 4 5 {
    local j = `j'+1
    // omit North (lack of observations) so adjust local index
    if (`i' == 3) {
    local j = `j'+1
    }
    mata: foo()
    }
    Code:
    . clear all
    
    . 
    . mata: 
    ------------------------------------------------- mata (type end to exit) ----------------------
    : function foo () {
    >         gnxl = 42
    >         gnxl
    > }
    
    : end
    ------------------------------------------------------------------------------------------------
    
    . 
    . local j = 0
    
    . foreach i of num 1 2 3 4 5 {
      2. local j = `j'+1
      3. // omit North (lack of observations) so adjust local index
    . if (`i' == 3) {
      4. local j = `j'+1
      5. }
      6. mata: foo()
      7. }
      42
      42
      42
      42
      42
    
    .

    Comment


    • #3
      thanks, that was very helpful

      Comment

      Working...
      X