Announcement

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

  • Specifying optional and required options in a simple program

    In researching the above question, I came across the this reply from Maarten Buis in 2007:
    If you make the option -i()- optional you have to specify a default
    value, so instead of your syntax command you need to write:

    syntax [varlist], a(integer) b(real) c(real) d(real) e(real) ///
    f(real) g(integer) h(integer) [i(integer 5)]

    if you want the default to be 5.
    But its is also possible to capture the lack of option -i()- and substitute within the programme:
    Code:
    if (`i' == "") {
      i = 5
    }
    Are these two equal approaches to the same end, or does one represent better practice that the other?
    Stata 14.2MP
    OS X

  • #2
    I have some trouble parsing your English. If your first language is English, please proof-read more carefully.

    You can do something like this

    Code:
    syntax , [ i(numlist int max=1) ] 
    
    if "`i'" == "" local i = 5
    So the i() option can take a numlist, but if none is specified, the resulting macro is empty and you can then insert a default if and as you wish. Here we're exploiting the fact that the macro has string contents, but we can feed it a number and Stata won't complain.

    You could also have i() as an optional string option and test similarly to see if it was specified. That way looks simpler, but you'd have to do more error checking.

    Your putative syntax wouldn't work as a string comparison requires " " on both sides and a bare assignment i = 5 is not supported in Stata (Mata, yes).

    Comment


    • #3
      Hi Nigel,

      If you don't specify an i() option within your syntax statement at all, then it will always contain nothing (unless defined in some other way), and your if-statement is redundant.

      However, if for some reason you wish to avoid specifying a default, you could instead code:

      Code:
      syntax [varlist], a(integer) b(real) c(real) d(real) e(real) f(real) g(integer) h(integer) [i(string)]
      ...and then check whether what was passed to the i() option was an integer or not within the program (e.g. using assert).

      Does that help?

      Best wishes,

      David.


      EDIT: Nick's reply, which crossed with mine, suggests the ingenious alternative [i(numlist int max=1)] in place of [i(string)].
      Last edited by David Fisher; 14 Sep 2017, 05:25.

      Comment


      • #4
        Nick, David

        thank you both!

        Best

        Nigel
        Stata 14.2MP
        OS X

        Comment


        • #5
          hi,
          I'm create a program with just options, I mean
          Code:
          program define AR_sim 
          syntax , [LENgth_vector(numlist int max=1) LAG_p(numlist int max=1) Mean_ra SD_ra(numlist int max=1)  COEFficient INt_Value(numlist int max=1) ]
          
          if "`length_vector'"=="" local length_vector = _N
          if "`lag_p'"=="" local lag_p = 1
          if "`mean_ra'"=="" local mean_ra = 0
          if "`sd_ra'"=="" local sd_ra = 1
          if "`coefficient'"=="" local coefficient = 0.5
          if "`Int_Value'"=="" local Int_Value = 1
          
          ...
          end
          The code run, but when I try to especify the options:

          Code:
          AR_sim , [LEN(20) LAG(5) M(0)]
          Occur a error:
          Code:
          option [ not allowed
          r(198);
          There is posible define a program with just options?

          Stata 15MP
          OS Windows 10

          Comment


          • #6
            The syntax you define with the syntax command differs from what you later type. With the syntax command, you type square brackets to denote optional arguments. With few exceptions, you do not type square brackets when you later call a command. Also with the syntax command, you use uppercase letters in options to denote minimal abbreviations. With few exceptions, options are later typed in lowercase letters.

            Type

            Code:
            AR_sim , len(20) lag(5) m(0)
            That will produce a syntax error, though, because option Mean_ra does not accept arguments. You will have to correct this in the syntax command.

            For your example, you could use optional integers, as in

            Code:
            syntax [ , LENgth_vector(integer `=_N') LAG_p(integer 1) ... ]
            That way, you do not need to set defaults later.

            Comment

            Working...
            X