Announcement

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

  • How to nest a chunk of Mata code inside a Stata program

    As shown in the code below, a chunk of Mata code in a Stata do file works well. If you create a Stata program, a single line of Mata code works inside the Stata program, but if you try to nest a chunk of Mata code inside the Stata program, it gives an error. I think Stata mixes the "end" of the Mata chunk with the "end" of the Stata program. I know that it is possible to take the chuck of Mata code outside the Stata program and write it as a Mata function to be called inside the Stata program in a single Mata line. But is it really the only way to do it, or is there a simple trick that I am not aware of to get the chunk of Mata code work right inside the Stata program?

    Code:
    //chunk of Mata code works here
    mata
        for(i=1;i<=10;i++) {
            A=i
            B=i+1
        }
    end
    
    cap program drop yeah
    program yeah
        
        //
        //chunk of Stata code here
        //
    
        //single line of Mata code works here
        mata: C=1
        
        //chunk of Mata code inside the Stata program doesn't work
        mata
            for(i=1;i<=10;i++) {
                D=i
                E=i+1
            }
        end //to end the Mata chunk above
        
        
    end //to end the Stata program
    
    yeah

  • #2
    Hi Cyrus,

    Probably the best way to do this for custom Mata code is to define it as a function outside of the Stata program and call it from the Stata program you create.

    This is not _exactly_ the code you had above, but should demonstrate how to get what you're hoping to do to work across both environments (Stata and Mata). I've used this in a couple of the programs I've posted to SSC and it seems to work fairly well.

    Code:
    clear all
    
    mata:
    
     void mata_loop(numeric scalar C) {
     
        numeric scalar B
     
        B = C
     
        for (i=1; i<=10; i++) {
        
            B = B + i
     
            display(strofreal(B))
            
            displayflush()
            
        }
        
    }
    
    end
    
    program yeah
        
        mata: mata_loop(1)
        
        
    end
    
    yeah
    - joe
    Joseph Nicholas Luchman, Ph.D., PStatĀ® (American Statistical Association)
    ----
    Research Fellow
    Fors Marsh

    ----
    Version 18.0 MP

    Comment


    • #3
      Hi again Cyrus,

      I see now that you already knew about the method I note above.

      Apologies for regurgitating a known solution. Missed that note in my initial read of your question.

      In short, I don't think there is a way to avoid Stata reading the -end- without terminating the program. Perhaps folks at StataCorp know better?

      - joe
      Joseph Nicholas Luchman, Ph.D., PStatĀ® (American Statistical Association)
      ----
      Research Fellow
      Fors Marsh

      ----
      Version 18.0 MP

      Comment


      • #4
        This code works
        Code:
        cls
        cap program drop yeah
        program yeah
            
            //
            //chunk of Stata code here
            //
        
            //single line of Mata code works here
            mata: C=1
            
            //chunk of Mata code inside the Stata program that works
            mata {
                for(i=1;i<=10;i++) {
                    D=i
                    E=i+1
                }
            } //to end the Mata chunk above
            
            
        end //to end the Stata program
        
        yeah
        mata D, E
        But I think that Joseph Luchman's solution is better and more stable
        Kind regards

        nhb

        Comment


        • #5
          That really worked, Niels! Thank you so much for this short-cut. I agree that Joseph's way is the formal way to do it and I mentioned that my original post too. But every once in a while, you just need the Mata chunk run within your Stata code so that you can tinker with it without the need to scroll up and down within the whole program. Your solution works for that purpose, so thanks a lot!

          Comment

          Working...
          X