Announcement

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

  • Help with ado file created command options

    Hello Statalist Users,

    I have two general questions about creating commands in Ado files. I am just learning to make my own commands in Ado files.

    I am using Stata 15.1 on Windows 10.

    #1: QUESTION #1

    I would like one of the options in the command to be constrained to one of four strings that I specify in the program, for when the user specifies the option. An illustrative example follows.

    program animal
    version 15.1
    syntax varlist(max=9999 string) [, valuein(varname) skip(numlist max=1) move(string)]
    end

    So a command calling that program might look like the following.

    animal(v1 v2 v3), valuein(dog) skip(3) move(up)

    If I put something in the brackets associated with "valuein" that isn't a variable, I'll get a Stata produced error message, and Stata will stop running, a good thing / exactly the response I want.

    If I put something in the brackets associated with "skip" that isn't a number, I'll get an error message, and Stata will stop running, a good thing / exactly the response I want. If I put two numbers into the brackets associated with "skip", I'll get an error message, also exactly the response I want.

    What I don't know how to do is to constrain the strings that can be put into the brackets associated with the option "move" to just four strings: "up", "down", "left" and "right". I want to receive a Stata produced error message if something else is put inside those brackets, even if it is a string. Furthermore, if I put two or more of those four strings in, I also want to receive an error message.

    What is the correct syntax to do this?

    I am able to write my own syntax to verify that one of those four strings was input into the option "move" later in the program than the syntax line, but I want to know how to do this with the tools that Stata makes available, if they exist. I'm guessing there is a concise way to constrain the strings that can be used in a particular option in the syntax line.

    #1: QUESTION #2

    The default is for "options to be optional". In other words, if I type the command

    animal(v1 v2 v3), valuein(dog)

    no error message will result, since I can omit the options "skip" and "move" in the command.

    How do I specify that an option is required? In other words, if the option isn't specified, an error command will result?

    Thanks for the help,

    Carl

  • #2
    1) To restrict the number of arguments for move(), you could use
    Code:
    move(namelist max=1)
    .
    2) [option] is optional, without the brackets the option is not optional. You can test this using:
    Code:
    prog def testIT
    
    syntax , move(namelist max=1) [skip(numlist max=1)]
    
    local move_valid = `" "up", "down", "left", "right" "'
    capt assert inlist( "`move'", `move_valid')
    
    if _rc {
        
        di as err `" Valid args to option move(): `move_valid' "'
        exit
    }
    
    di "`move'"
    
    end
    Code:
    . testIT , move(up)
    up
    
    . capt noi testIT , move(up down)
    option move():  too many names specified
    
    . capt noi testIT , move(away)
    Valid args to option move():  "up", "down", "left", "right"
    Last edited by Bjarte Aagnes; 07 Feb 2019, 10:49.

    Comment


    • #3
      Hi Bjarte, Great, the code you provided was extremely useful, thank you! Carl

      Comment

      Working...
      X