Local and Global Macros in Stata

In a previous article, we discussed scalars and matrices in Stata, and how they help us extract results after an E-Class or R-Class command is run. This article will go over an associated topic i.e. Stata Macros.

Before we explore further, here’s a quick guide for you:

In Stata, macros are tools that store values for reuse. There are two types: local and global. Local macros have a temporary scope, only existing within the specific command or do-file where they’re defined.

They are great for transient tasks in a script. Global macros, on the other hand, persist across different commands and do-files until the Stata session ends.

They’re useful for defining values or lists that need to be accessed repeatedly throughout an analysis. While local macros are referenced with a backtick (`) and apostrophe (‘), global macros use the $ symbol.

Keep reading article for in-depth guidance!

What Are Macros in Stata?

Download Example File

Macros are somewhat similar to scalars: they store values. The role of macros in reproducibility in Stata analysis. In this article we will compare the efficiency of using macros vs. not using them. There are two types of macros: global and local.

Stata’s Global Macros:

Global macros work regardless of what workspace they are being run in i.e. anyone will be able to run a global. If you write a global in a do-file and send it to your friend, they will be able to run it just fine on their end as well.

To define global macros, we use a familiar syntax:

global xlist mpg weight

Here, xlist is the name we have given to this particular global which, in this case, saves a list of two variables mpg and weight. Globals can also store numeric values and strings.

A global storing a list of variables can be used in, for example, a regression. Let’s say that we want to see the effect of a car’s mileage and weight on its price. To run the regression, we can use the previously created global:

reg price $xlist

Note that when referring to an existing global, we precede its name with the $ symbol.

The above command is exactly the same as executing:

reg price mpg weight

In cases where the list of our independent variables is very long and multiple regressions need to be run, using globals removes the need to write the list of variables over and over again, and also maintains the readability of our do-files. In case you need to add or drop some control variables from the regressions, you would only have to edit the global rather every regression command.

We can also use the global to summarize the list of variables it contains:

summarize $xlist

If need be, you can even store a single variable in a macro. In this case, we can store price in a new macro called ylist and run the same regression as above.

global ylist price reg $ylist $xlist

Another use for globals comes up when we wish to apply some method to our analysis in order to deal with problems like, for example, hetersckedasticity. In such a case, we would want robust standard errors to be calculated to address the problem. We can define a macro that does this by defining it and calling it ‘robust’ (which is the option used alongside regression commands to get robust standard errors):

Related Book: Data Management Using Stata by Michael N. Mitchell 
global method robust reg price mpg weight, $robust

In case this method was to be replace with another one, or if more options needed to be added across multiple regressions, we would only have to change the global called ‘method’.

Finally, globals can also be used to store file paths. If your datasets are stored in different folders, you can store the common part of their directory in a global.

global datafolder “E:\ABC” use “$datafolder\analysis.dta”, clear

In this manner, we can also save other (usually longer) directories once in their respective macros, and then reference them throughout our do-file without retyping them and still maintaining good readability.

Stata’s Local Macros:

Local macros are also used for storing some piece of data, but they only work for the programme currently being run. Unlike global macros, they will only work until the existing do-file is being worked on, and will not work if any other do-files are active.

Locals can be used to store mathematical expressions with a slight change in the syntax we are already familiar with.

local local1=5+5

The equality sign tells Stata to store the evaluated result of the expression we specify (instead of storing that expression as a string). In this example, the local called local1 will store the value 10.

If you wanted to store the above as a string, the expression would be enclosed in inverted commas without the equality sign.

local local1 “5+5”

In subsequent examples, we will go with the first definition of local1 where it was defined as a mathematical expression equalling 10.

To display a local, we refer to its name after the display command while enclosing it in a backtick (`) and an apostrophe ('). The backtick symbol can usually be found on the key with the tilda sign (~) on your keyboard.

display `local1'

When you execute the display command on its own, you will notice that it does not show any result. This is because any command that makes use of a local has to be run simultaneously with the command where that local was initially defined. In order to display an answer of 10, we have to select both of these commands and execute them together.

local local1=5+5 display `local1'

When run together, Stata will display an output of 10.

This is unlike global macros which continue to exist in Stata’s memory when they are run the first time. If however, you are using the command box to write and execute commands (rather than a do-file), entering the two commands separately will give you an output of 10 as well.

In case this were a string, the display command would also require inverted commas in the following manner:

local local1 "5+5" display "`local1'"

We can also add a list of variables to a local and use it in a regression or a summarize command. As before, the two commands will have to be run simultaneously.

local loclist mpg weight reg price `loclist'

Locals can also be used in a loop to refer to a list of numbers, strings or variable names. Here, let’s use the local loclist and generate two new variables through a loop that will be equal to the original variables contained in the local.

foreach var in `loclist'{          gen new_`var' = `var' }

Now this is something that is also possible through defining a scalar. However, scalars are not stored in Stata’s memory if the clear all command is used. Let’s illustrate this by defining a global macro, a local macro, and a scalar taking on the values ‘1’, ‘2’, and ‘3’ respectively.

global a=1 local b=2 scalar c=3 clear display $a `b'  c 

In this case, when only the clear command is used, Stata is able to display the values for all three:

Stata's local macro example

When the clear all command is used, the scalar is no longer displayed.

global a=1 local b=2 scalar c=3 clear all display $a `b'  c 
error  in local macro in stata

This is because the clear all command effectively removes all scalars from Stata’s memory, while the globals and locals remain.

Another advantage of using local and global macros is that they are immediately recognizable due to the $, apostrophe and backtick signs used to indicate to them. In the above example, it is not immediately apparent what the c is.

The art of data manipulation requires careful articulation of commands. So, as a last piece of advice, we should give a more descriptive name and consider best practices for naming and managing macros in larger projects, so that we can recognize them if someone else is using the program.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Tweet
Share
Share
Pin