Search code examples
statastata-macros

Preseting mandatory string options in syntax with default value


Consider the following toy program:

program define table_v1.01, rclass
version 15
syntax varlist(fv min = 1 max = 1) [if] [in], pat_ID(string) = Patient_ID
end 

I would like to make the option pat_ID compulsory.

In addition, since 90% of the time its value will be Patient_ID, how can I set this as a default value?


Solution

  • The following works for me:

    program define foo, rclass
    
    syntax varlist(fv min = 1 max = 1) [if] [in], [pat_ID(string)]
    
    if "`pat_ID'" == "" local pat_ID Patient_ID
    display "`pat_ID'"
    
    end
    

    Example:

    . foo i.foreign
    Patient_ID
    
    . foo i.foreign, pat_ID(Other_ID)
    Other_ID
    

    Unlike other types of options (for example those accepting integers or reals), you cannot directly set a default value for strings. Typing help syntax will provide you with information about what is possible.