0

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.

kathystehl
  • 831
  • 1
  • 9
  • 26
  • 1
    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 Mar 04 '17 at 18:14
  • 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 Mar 04 '17 at 18:28
  • @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 Mar 04 '17 at 19:25

2 Answers2

1

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.

  • Could you offer some more context? I do not receive the same error, at least when I run the code that you gave line-by-line (not in a .do file). `display` simply prints the first element of my macro as one would expect. – kathystehl Mar 04 '17 at 16:34
  • I'm not sure what the differences are our code, but I don't receive the error when I run the code that you've given line-by-line or in a .do file. I only receive the error when I load in a new .dta file (manually or in the same .do file). The error results after I: (1) define the macro, (2) load in a new .dta file, (3) run the code up through `display "``macro_name'"` (regardless of it being done manually or in .do file). The code that you've listed runs fine for me. I based my locals.do proposal off this note from Stata Journal: http://ageconsearch.umn.edu/bitstream/143005/2/sjart_pr0047.pdf – kathystehl Mar 04 '17 at 18:49
  • To clarify: when I do load in a new file before trying to call my macro, all of the code that you've given works except for `di `macro_name``. The root of my confusion is that the macro seems to persist in the sense that the connection between `macro_name` and `macro_variable` persists (displaying `macro_name` is quotes prints `macro_variable`), but when I try to display an element of `macro_name` with the last line of code, I receive the error that `macro_variable` is not found. – kathystehl Mar 04 '17 at 19:03
  • I suggest you edit your original post (which I believe remains possible) and add a minimal complete verifiable example as described at http://stackoverflow.com/help/mcve that others can then use to understand and reproduce your experience. Your clarifications are not clarifying things, for me at least. Regarding your included file, you are correct, I overlooked the fact that you used `include` rather than `do` or `run`. –  Mar 04 '17 at 20:27
  • Thanks for your help @WilliamLisowski.I think that I figured it out through my comments with Nick and posted an answer below. – kathystehl Mar 04 '17 at 20:37
0

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.

kathystehl
  • 831
  • 1
  • 9
  • 26
  • 1
    That was a very fundamental misunderstanding. I would suggest that you need to improve your understanding of macros by reading section 18.3 of the Stata User's Guide PDF included in your Stata installation. The second sentence within that reads "A macro is a string of characters, called the macroname, that stands for another string of characters, called the macro contents." –  Mar 04 '17 at 21:00
  • 1
    Sorry, but this makes little or no sense to me as a Stata user or programmer. Downvoting it would seem harsh but I flag that impression for any future readers. – Nick Cox Mar 05 '17 at 08:47
  • Thanks and apologies @NickCox. Not sure how to rephrase to make the concept more clear. To future readers, do read 18.3 in the Stata User's Guide. I'm obviously not getting the concepts at this point, but hopefully you will based off info in the guide. – kathystehl Mar 05 '17 at 09:03
  • There is no necessary connection between a macro name and the variables whose names it may contain except in the mind of the programmer. Macros are just macros. I don't know what else you may be saying or thinking. Without a concrete example I can't suggest further clarification or correction. – Nick Cox Mar 05 '17 at 09:09