Announcement

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

  • Why does Mata say the first argument of st_view is not found? Do I need to declare the variable beforehand?

    I have a simple do-file that contains a program, which itself contains some Mata syntax:

    Code:
    capture program drop take_log
    program take_log
        syntax varname [if] [in], Generate(name)
        marksample touse
        quietly gen `generate' = .
        mata:
            st_view(v, ., "`varlist'", "`touse'")
            st_view(logv, ., "`generate'", "`touse'")
            logv[., .] = ln(v)
        mata end
    end
    
    
    cls
    set more off
    sysuse gnp96, clear
    take_log gnp96, g(log_gnp96)
    When I run this, I get an r(3499) error of "<istmt>: 3499 v not found" on this line:

    Code:
    st_view(v, ., "`varlist'", "`touse'")
    Based on my reading of the documentation for -st_view()- (http://www.stata.com/manuals13/m-5st_view.pdf) this syntax should be correct, since I shouldn't have to declare the matrix -v- beforehand. Am I reading that right? What's the problem here?

    And yes, I know this is a silly example of a do file that could easily be rewritten without Mata, e.g. like this:

    Code:
    capture program drop take_log
    program take_log
        syntax varname [if] [in], Generate(name)
        marksample touse
        gen `generate' = ln(`varlist') if `touse'
    end
    
    
    cls
    set more off
    sysuse gnp96, clear
    take_log gnp96, g(log_gnp96)
    but this started out as a minimal working example I was using to figure out another error message, and it's a simple example of a larger program I'm working on.

    Thank you,
    Michael Anbar
    Last edited by Michael Anbar; 23 Sep 2014, 11:59.

  • #2
    I fixed the error like this:

    Code:
    mata: mata clear
    
    capture program drop take_log
    program take_log
    syntax varname [if] [in], Generate(name)
    marksample touse
    quietly gen `generate' = .
    mata:
    st_view(v = ., ., "`varlist'", "`touse'")
    st_view(logv = ., ., "`generate'", "`touse'")
    logv[., .] = ln(v)
    end
    
    end
    
    cls
    set more off
    sysuse gnp96, clear
    take_log gnp96, g(log_gnp96)
    but where is this syntax documented? I don't see it in the documentation for -st_view()-. Now, however, I get an error saying "unrecognized command: end", but I believe that's related to the issue discussed in this post (http://stackoverflow.com/questions/1...a-in-ado-files), in that I shouldn't put the mata block within the program block. unc

    Comment


    • #3
      v and logv have to exist before you can use st_view
      Try
      Code:
      st_view(v=., ., "`varlist'", "`touse'")
              st_view(logv=., ., "`generate'", "`touse'")
      or
      Code:
      v = logv = .
      st_view(v, ., "`varlist'", "`touse'")
              st_view(logv, ., "`generate'", "`touse'")
      Christophe

      Comment


      • #4
        Right, I figured out that syntax (see my reply). Where is this documented? I don't see it in the documentation for -st_view()-.

        Comment


        • #5
          Could you please take a look at: Gould, W. W. 2005. Mata Matters: Using views onto the data. Stata Journal 5: 567-573.

          It is available here: http://www.stata-journal.com/sjpdf.h...iclenum=pr0019

          Comment


          • #6
            Thanks for pointing me to that; the abstract of that article (which appears when using -search st_view-) includes a link to the full PDF of the paper, but why this syntax isn't prominently included in the actual documentation is beyond me. This is an extremely basic use of the st_view function, so much so that the examples in the main documentation for -st_view()- don't actually work without knowing about this syntax. Doesn't it make sense to have something so crucial to the use of a function prominently displayed in the main documentation, instead of buried in a journal article somewhere?

            Look at the documentation for competitors of Stata, e.g. MATLAB, Python, even something as archaic as RATS. All of them including actual, working examples, not snippets that don't work without looking at another source (Stata is usually pretty good about this, and at least in this case, the main documentation links to the article you referenced, which does help).

            Comment


            • #7
              Originally posted by Michael Anbar View Post
              Thanks for pointing me to that; the abstract of that article (which appears when using -search st_view-) includes a link to the full PDF of the paper, but why this syntax isn't prominently included in the actual documentation is beyond me.
              I'm not sure what you mean by "this syntax"—calling a function with an unknown argument would fail in any language (i.e., how would the call proceed?). Think of it this way: arguments are passed by address in Mata, so if you call a function with an unknown argument, then there is no address with which to work. I suppose st_view() could have been written the following way instead:
              void st_view(name, real matrix i, rowvector j, scalar selectvar)
              where name is a string scalar, and where the function itself created a matrix with this name to store the result. However, there are some disadvantages to this, which is probably why it wasn't done that way. In any event, this issue is well documented at the very beginning of the Mata Reference Manual under [M-1] Introduction and advice (see the Section on returnedargs).

              Comment

              Working...
              X