Announcement

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

  • Mata Arguments and date funtion

    Hello everyone
    I am trying to define a mata function that will cause Stata to exit a program if the current year is greater than 2016. I have tried the following but have failed, with the error message
    nothing found where 'while' expected
    r(3000);

    . Can somebody make the code work for me.
    Code:
    mata
    void expired()
    {
    stata(`"if year(date("$S_DATE", "DMY"))>2016{"')
    stata(`"display as error "The Linces has expired, please renew it ""')
    stata("exit")
    stata("}")
    end
    }
    mata mosave expired()
    end

  • #2
    The current date will now always be greater than 2016. That has nothing do with whether any "Linces" (?licence) has expired.

    Comment


    • #3
      Nick I understand that, it is for testing the function. Once the function is properly defined, I shall then change the value to 2017 or so.Right now the problem is I cannot figure out what is wrong with the code?

      Comment


      • #4
        I don't have experience with expired licenses, but I have guessed that Stata would just not run any way.

        So I still don't understand your real problem, now that you have admitted that what you were asking was not the real problem

        Writing your own function that takes in some date as input would seem more likely to succeed.

        Comment


        • #5
          Thanks for your reply. My real problem is that the above code has technical issues. I cannot pass the Stata argument to Mata, See this simple line
          Code:
          stata("if year(date("$S_DATE", "DMY"))>2016{")
          , it returns the error
          invalid expression
          r(3000);
          OR even simple
          Code:
          mata
          stata(" dis "$S_DATE" ")
          will throw the same error, where
          Code:
          dis "$S_DATE"
          perfectly works in Stata
          Perhaps, the double quotes around the $S_DATE and around the whole Stata argument create problems. So my simple question is "How can we use double quotes when we want to pass arguments to mata from a Stata command?
          Last edited by Saeed Sardar; 10 Jan 2017, 07:00.

          Comment


          • #6
            Turns out that
            Code:
             stata(`" dis "$S_DATE" "')
            works fine. However, the above code still returns the error,
            nothing found where 'while' expected
            r(3000);
            Last edited by Saeed Sardar; 10 Jan 2017, 07:10.

            Comment


            • #7
              Like Nick, I don't really understand what you're up to, but

              1. have you determined that you can execute multiline Stata commands via stata()?

              2. what's end doing inside your function?

              Nick's suggestion seems to be a more straightforward way to approach Mata's checking a data against a reference date and then acting upon it.

              Comment

              Working...
              X