Stata: Check if a local macro is undefined
Asked Answered
S

3

9

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.

Suburb answered 7/3, 2011 at 6:3 Comment(0)
D
9

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"'.

Dairying answered 7/3, 2011 at 18:47 Comment(3)
I don't think there's an "official" way to do this kind of thing. You can read the .ado files that come with Stata (in C:\Program Files\...) They do different things for this kind of test. You might be interested in the cond() function, too.Dairying
I failed to specify this in my original question, but I'm actually trying to check if a macro name is not undefined. It turns out that if !missing(`macroname') { works for this. Thanks so much!Suburb
This is entirely equivalent to my later answer, just one step more indirect. For strings, empty counts as missing, so a test for missing is precisely the same test as a test for being empty.Apgar
A
7

The question asked for an idiomatic way to do this and across Stata programmers

 if "`macroname'" != "" 

is far by the most commonly used test of whether a macro is defined. Macros contain strings when they are defined, and this is the general usage; use of numeric characters is just a special case.

Apgar answered 6/7, 2013 at 10:38 Comment(0)
F
3

OP asked if there was a way to test if a local is undefined. The concept of defined/undefined doesn't really exist in Stata (which is confusing to users new to Stata but not new to programming).

A more Stata like way to think about this is if the local is missing or not missing. If a local has not been defined it is considered missing. But if a local has been defined to a missing value, like the empty string "", it is also considered missing.

So, there is no way in Stata to differentiate between a string local being undefined or being defined but contains a missing value, i.e. the empty string "". Both the answers already given does not capture the difference between localA (defined but as the empty string) and localB (undefined) in the example below:

*initiating localA to the empty string
local localA ""

*Both these conditions will evaluated identically
if missing("`localA'")
if missing("`localB'")

*Both these conditions will evaluated identically
if "`localA'" != "" 
if "`localB'" != "" 
Faker answered 17/9, 2020 at 17:15 Comment(1)
Thanks for the edit. It's more subtle yet. For example, all locals are really strings, or contain strings. So, the string "." emphatically does not mean missing, nor is it equal to missing, as string expressions are considered as missing if and only if they are empty strings.. But nevertheless once out of a macro and in context a bare period can mean numeric missing. You know that, but it is something else beginner programmers need to grasp.Apgar

© 2022 - 2024 — McMap. All rights reserved.