This can cause headaches: if you mistype a macro's name you'll probably get a generic syntax error with no indication that a macro is the cause of the problem. Stata's macro processor just replaces it with nothing: Unfortunately, using a macro you haven't defined doesn't generate an error message. This can be extremely useful: for example, if you had a macro `year' containing the current year, `=`year'-1' would be the year before the current year. This tells the macro processor to subtract one from the value of the macro x and then place the result in the code. Macro expressions-and macros in general-can contain other macros. Another common use is `=_N', which will be the number of observations in the current data set (and can be used in places where _N by itself can't). Instead, the equals sign before 2+2 told the macro processor to evaluate that expression and put the result in the code, so what Stata proper saw was display "4". The result is 4, but display didn't calculate it (the quotes prevent that). Where expression is the expression to be evaluated. any formula you could put after the equals sign in a generate or replace command (but not egen). Stata's macro processor can evaluate Stata expressions i.e. Now x really does contain 4 and not 2+2 no matter how you display it. If the local command contains an equals sign, Stata will evaluate what follows before putting it in the macro. If you want to put the result of a calculation in a macro, put an equals sign after the macro name: This feature allows you to use macros absolutely anywhere, even in macro definitions. In fact Stata proper didn't know or care that 2+2 came from a macro.
But consider what happened before you put it in quotes: your macro contained a working bit of Stata code which Stata happily executed when you used it. Then display will treat it like a string. If you want display to put something on the screen without evaluating it, put it in quotes. So it evaluated 2+2 and gave you the answer. The result is 4, but that's because the display command acts like a calculator.
Now try a slightly more complicated macro: When it sees a macro (denoted by that particular set of quotation marks) it replaces the macro with its table. Macros are handled by a macro processor that examines commands before passing them to Stata proper. It is found under the double quotation mark ( ") on the right side of the keyboard. The quote after the x is the right single quote. It is found in the upper left corner of the keyboard, under the tilde ( ~). The quote before the x is the left single quote. To use a macro, you put its name in a command, surrounded by a particular set of quotation marks: This creates a local macro called x and puts the character ' 1' in it (not the value 1 as in "one unit to the right of zero on the number line"). If not, just trust us that local macros are the right ones to use. If you're familiar with global and local variables from other languages, Stata's local macros are local in the same way. (The real trick is getting a single command to run multiple times with a different bit of text in the box each time-we'll get there). You then use what's in the box in subsequent commands.
MacrosĪ Stata macro is a box you put text in. Typing the commands in the examples yourself will help you notice and retain all the details, and prepare you to write your own code. This article is best read at the computer with Stata running.
If you've done a lot of Stata programming already and are looking to expand your "bag of tricks" check out Stata Programming Tools. The primary intended audience is Stata users with no other programming experience. To benefit from this article you'll need a solid understanding of basic Stata syntax, such as you can get from our Stata for Researchers series. This article will focus on those programming tools that, in our experience, anyone who uses Stata heavily will eventually want to learn. Stata has all the tools required to write very sophisticated programs, but knowing just a few of them allows you to make everyday do files shorter and more efficient. If not, someday you will-so you might as well keep reading anyway. Ever needed to do the same thing to ten different variables and wished that you didn't have to write it out ten times? If so, then this article is for you.