kathystehl
kathystehl

Reputation: 841

Macro lost after reading in new file

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.

Upvotes: 0

Views: 449

Answers (2)

kathystehl
kathystehl

Reputation: 841

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.

Upvotes: 0

user4690969
user4690969

Reputation:

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.

Upvotes: 1

Related Questions