Announcement

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

  • Problem with invalid syntax

    Dear All,

    I am trying to write my own (first) program. However, when I run it I get the message that the syntax is invalid. I presume that I am making a mistake in the line of code where I set syntax. The line is:

    Code:
    syntax varlist(min=6 max=6 numeric), INTeraction(min=2 max=2 numeric) [ MInimum(real -.12345) MAximum(real .12345) High Level(cilevel)]
    and the error message is:

    Code:
    invalid syntax
    r(197);
    Basically I want to run a test over six variables (hence I set varlist min=6 and max=6 numeric). The I would like a compulsory argument, where I would like to indicate the two variables that I use in the analysis as interaction terms (hence I set INTeraction(min=2 max=2 numeric) ). All the rest are options.

    What is wrong with that line of command? Or the error message may depend on other lines of the program?

    Thanks in advance for your help.

  • #2
    I can't see a syntax error here myself. In my experience 197 does imply a problem with the syntax command, not with the way you're calling the program.

    As gratuitous advice: Why write a program at all for anything so very specific? I would just write a do-file, possibly with arguments. My first programs grew out of do-files and a realisation that I really did need a command not a do-file, but that hinges on a need for generality and flexibility.

    Comment


    • #3
      Hi Nick, thanks for your advise. Your suggestion is wise. Actually, I opted for a do file at the beginning, but then I realized that I need to run a test (the program is for a specific test) for many models and I thought that it could be "easier" to set a program once for all to in order to avoid to write a very long do file. But I will think about your suggestion. Thanks again.

      Comment


      • #4
        Well, the problem is

        Code:
        , INTeraction(min=2 max=2 numeric)
        How is Stata supposed to know that option interaction() refers to a variable list? You want

        Code:
        , INTeraction(varlist min=2 max=2 numeric)
        Note that min and max do not check for repeated variable names.

        Edit:

        Also, avoid "magic" numbers like

        Code:
        MInimum(real -.12345)
        except that is really the default that you want. Better would be

        Code:
        MInimum(numlist max=1)
        ...
        if ("`minimum'" == "") local minimum 42 // <- insert the desired default

        Best
        Daniel
        Last edited by daniel klein; 30 Jul 2019, 13:20.

        Comment


        • #5
          daniel klein Well spotted!

          Comment


          • #6
            daniel klein Thanks a lot for your help. I also agree about the magic number too. In fact I am considering to change it, as actually 0.12345 could be a possible value that the function under analysis could take at the minimum/maximum.

            Comment


            • #7
              You don't need a default for either minimum or maximum options. Depending on details you don't tell us, one of these might be a good idea.

              1. You could ask for minimum and maximum to be given in one option as a numlist with two entries. Evidently the smaller of two values will be taken as the minimum, and so forth.

              2. You could ask for minimum and maximum to be given separately as numlists with precisely one entry each. You need this if a user might want to specify a minimum, but not a maximum or vice versa.

              Yet another alternative is to specify a default that is impossible, so that the program can check whether the user has set the option. For example, suppose that possible values must be positive. Then a default of -1 is safe (but should be ignored in your program).

              Comment


              • #8
                Nick Cox Thanks Nick.

                Comment

                Working...
                X