I am writing a Stata do
file and I would like to provide default values if the user does not supply some parameters. To do so, I woud like to check if a macro is undefined.
I have come up with a hacky way to do this:
*** For a local macro with the name value:
if `value'1 != 1 {
...do stuff
}
But I would like to know if there is a idiomatic way to do this.
If it's undefined, the contents of the macro would be empty. You can do this:
if missing(`"`mymacroname'"') {
display "Macro is undefined"
}
The quotes aren't really needed if the macro will contain a number. The missing(x)
function can handle strings and numbers. It is kind of like testing (x=="" | x==.)
Placing `'
around "`mymacroname'"
allows the macro to contain quotes, as in local mymacroname `"foo"' `"bar"'
.