Announcement

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

  • trouble shooting a mata function

    Dear all,

    I am having problems with a function embedded in a customized program. Below is the said function:

    real rowvector rs_max2(real matrix X)
    {
    real scalar j
    real rowvector xres
    real colvector v

    /*designate vector xres to temporarily store the results:*/
    xres = J(1, cols(X), .)

    /*get the index for the maximum:*/
    for(j=1; j<=cols(X); j++){
    maxindex(X[.,j], 3, v=., .)
    xres[1, j] = X[v[2],j]
    }
    return(xres)

    }

    I wrote this function to get the second smallest value for each column of a matrix X. It is part of an .ado program, which is very long. I believe the error occurs within this function because the error message I got when I execute the entire program is:

    'real' found where almost anything else expected
    (229 lines skipped)

    , and the 229 lines from the bottom of the entire program is the line: " real scalar j" or " real rowvector xres"

    I have scrutinized this function multiple times, but could not find an error. Could you please help me check which part of it is mistaken? Thank you very much in advance.
    Last edited by Larry Kong; 20 Jul 2019, 09:31.

  • #2
    If you don't have any problem with this function per se, then I'm guessing the problem isn't in this function. The problem's before it, and the "real" that the compiler's complaining about isn't in either of the two variable type definitions, but rather in the function type definition: real rowvector rs_max2(real matrix X) {.

    Check for an unclosed set of parentheses or an unclosed bracket pair above this function.

    Comment


    • #3
      I'd also say something about general strategy for finding errors in Mata: My preferred (though clunky) strategy is to simply put several text displays in my code (lines with just a string on them "spot1" "spot2" ... "spot3"), and use this to narrow down the location of the problem. (Aha, the problem is somewhere between "spot4" and "spot5"). I might start with a few widely spaced such items, and then progressively move them closer together on successive runs of the problem code.

      Comment


      • #4
        Thanks to both Joseph and Mike for your good suggestions.

        Now I have solved the current problem by deleting the comments enclosed by /**/'s. I guess that way of making comments are not right. But some more errors show up:

        rs_max2(): 3301 subscript invalid
        do_stats(): - function returned error
        <istmt>: - function returned error
        r(3301);

        I checked the subscript but cannot find any error. Could anyone help me with this new problem?

        Comment


        • #5
          problem solved guys. It turns out that I did not include the codes to deal with the contingency when the X[.,j] has missing value for all its elements. Lesson learnt that one should always care for this when writing a program. This is a key difference between writing a program and typing in commands interactively.

          Comment

          Working...
          X