Announcement

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

  • Mata Stata behave Strangly

    I am starting this new thread because the problem I was facing before has now changed. I am trying to define a mata function, that uses Stata's IF argument. I have managed to do it with some success, however, after the function is executed, and I return to Stata, it will show dot and not execute any command unless I press the close curly brackedt i.e. }. I do not understand why this happens

    Code:
    mata
    mata clear
    void f()
    {
    stata(`"if year(date("$S_DATE", "DMY"))<2016{"')
    stata(`"display as error "Ticket has expired, please renew it ""')
    stata("}")
    
    }
    
    mata mosave f()
    end
    mata: f()

  • #2
    I agree with Nick - I do not understand what it is for.
    Further I do understand why you insist of running everything within stata when you in fact are working in Mata.

    But I think this will help:
    Code:
    . mata
    ------------------------------------------------- mata (type end to exit) ----------------
    :         mata clear
    
    :         void f(real scalar year)
    >         {
    >                 if ( year(date("$S_DATE", "DMY")) > year ) {
    >                         printf(`"{error:Ticket has expired, please renew it}"')
    >                 } else {
    >                         printf(`"Everything is fine"')
    >                 }
    >         }
    
    : end
    ------------------------------------------------------------------------------------------
    
    . mata: f(2016)
    Ticket has expired, please renew it
    . mata: f(2018)
    Everything is fine
    Kind regards

    nhb

    Comment


    • #3
      Thank you Niels Henrik Bruun, you solution worked. I am not comfortable in mata, but need this mata function as it can be locked from reading, while Stata programs are open for all to see. I am not still getting what was wrong with my earlier post.

      Comment


      • #4
        Stata's command line language is interpreted, I believe, and so it waits for the closing brace. I'm not sure that Stata's interpreter will even return control to Mata until then. I recommend avoiding trying to execute mulitline Stata commands like that from Mata.

        As Nick and Niels have both suggested, it's better to work within Mata.

        Comment

        Working...
        X