Announcement

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

  • Specify default for string argument to Stata -program-

    I'm writing a Stata program, but having trouble specifying a default value for the string argument mi_type(). Here's a simplified version. When I don't specify a default, this runs fine:
    Code:
    program boot_mi_impute_mvn
     syntax varlist [, mi_type(string)]
    end
    boot_mi_impute_mvn attendance, rep(400) imp(2) mi_type(mlmi)
    But when I try to specify a default, this returns an error: "invalid syntax"
    Code:
    program boot_mi_impute_mvn
     syntax varlist [, mi_type(string mlmi)]
    end
    boot_mi_impute_mvn attendance gpa, mi_type(mlmi)
    Do you see what's wrong with my syntax statement? And can you tell me how to specify a default for the string argument -mi_type()? Many thanks.



  • #2
    The syntax command does not support default settings for string options. You will have to do that explicitly in the code.

    I would typically write something along the lines

    Code:
    program boot_mi_impute_mvn
        version 16.1
        
        syntax varlist(numeric) [ , MI_TYPE(string) ]
        
        if ( mi("`mi_type'") ) local mi_type "mlmi"
        else if ("`mi_type'" != "mlmi") {
            display as err "option mi_type() invalid"
            exit 198
        }
        
        ...
    end
    While not being able to specify a default value might seem inconvenient, I think it actually is not much of a problem; after all, you would certainly want to check for valid input later anyway (or at least you should do that).

    Just two more notes. First, note that I have included a version statement near the top of the program and so should you. Even if you are defining the program within a do-file in which you (should) include a version statement, you might later want to copy the program somewhere else. Including a version statement makes sure the program continues to behave in the same way in other places in the future. Second, I tend to avoid underscores in option names because the abbreviation rules for these option names are not obvious. In the example, even though I have typed all letters in uppercase (usually indicating minimal abbreviation), the option mi_type() may still be abbreviated as just mi() because of the underscore.
    Last edited by daniel klein; 21 Oct 2020, 23:52.

    Comment


    • #3
      A equivalent way of looking at this is to explain in your help that there is a default and explain that the point of the option is to change it if desired. Then the code can run that if the option was not specified, then there is an empty argument which can be replaced with the default value.


      Code:
      if "`foobar'" == "" local foobar "default value"
      This doesn't affect the point made clearly by daniel klein that checking of what is supplied may well be needed.


      Otherwise put, the test for a string being empty is the same as the test for whether it is missing.
      Last edited by Nick Cox; 22 Oct 2020, 01:25.

      Comment

      Working...
      X