0
votes

Using Stata, I define a local macro (macro_name) as a variable (macro_variable) in one data file.

After reading in a new file (in the same do file), I'm no longer able to reference that macro.

Instead, I receive the error:

. di `macro_name'
macro_variable not found

I am learning how to use macros, so please bear with me. However, shouldn't I be able to still display or call on that macro in a single do file even if I load in a new data set?

For example:

use "newdata.dta", clear

This problem occurs regardless of whether I define the macro as a global or local. Additionally, I attempted to solve the problem by creating a separate locals.do file that include in the preamble of my master do file as:

include locals.do

But, I still receive the error listed above.

Do macros (local or global) disappear immediately upon reading in a new file? That doesn't seem right based on what I've read.

Thanks in advance for any clarification.

2
Macros can't be stored with datasets. They aren't part of the data any more than your commands or programs are. Textual information can be stored in characteristics. - Nick Cox
Thanks for your comment @NickCox. I understand that macros aren't stored with datasets. Rather, my understanding of what happens when I create a macro using a variable is that it stores a vector of that variable's row entries in memory that I can reference with the name to which I have assigned that macro. Furthermore, that a local macro should persist within a single .do file and a global macro should persist across .do files and at the command line as long as I am running the same Stata session. Are those assumptions correct? - kathystehl
@NickCox I think that I get it now - macros only maintain the connection between macro_name and macro_variable and, therefore, macro_variable has to be in memory (i.e. the dataset that contains `macro_variable has to be in memory) for me to be able to access it via the macro, correct? In that case, is there any way to keep a vector of values (row entries of a variable) in memory as I change data sets? - kathystehl

2 Answers

1
votes

Consider the following, which points to the source of your problem, and in the last command, reproduces precisely the error message you received.

. do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD08491.000000"

. local macro_name macro_variable

. macro list _macro_name
_macro_name:    macro_variable

. display "`macro_name'"
macro_variable

. display `macro_name'
macro_variable not found
r(111);

end of do-file

Added in edit: The above was run from the do-file editor window. When I instead launch Stata and paste the four commands into the command window, running them a line at a time, the following are what results.

. local macro_name macro_variable

. macro list _macro_name
_macro_name:    macro_variable

. display "`macro_name'"
macro_variable

. display `macro_name'
macro_variable not found
r(111);

.

At the risk of over-explaining, the point to my original answer is that the error message the displayed in the original post, and in the final command in both of my examples, was due to the failure to include quotation marks in the display command, which caused display to believe that "macro_variable", which was the value assigned to the local macro "macro_name" was not a character string constant, but rather a variable name or scalar, and display was unable to locate a variable or scalar by that name.

Let me add as a bonus explanation that the use of locals.do described in the original post has no hope of working, because local macros are local to the do-file in which they are executed, and vanish at the termination of that do-file. In particular, if you submit a local command by selecting a subset of the lines in the do-file editor window, those lines are copied into a temporary do-file and the values of the local macros vanish at the termination of the temporary do-file.

0
votes

Generalizing what I wrote in my comment above to Nick:

Macros only maintain the connection between the variable/varlist assigned to a macro name and, therefore, the variable/varlist to which the macro's name refers to must be in memory (i.e. the dataset that contains the variable/varlist has to be in memory) in order to access it via the macro.

Assigning a variable/varlist to a macro does not persist the actual value(s)/element(s) in memory, but rather maintain the connection between the variable/varlist and the macro name assigned to it/them.