AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Does not equal sign stata12/28/2023 ![]() There is, however, a way to achieve that particular effect. The problem is that controls still has age and agesq. Suppose you run a few models and then decide to control for age using age5, a factor variable with age in five-year groups. Therefore changing the contents of age at a later time will not change controls. The macro age in the definition of controls is resolved at the time the macro is defined, not when it is evaluated. I used quotes to make the code clearer, but they are optional. The first ocurrence of age in the first line is the name of the macro, and the second occurrence the name of a variable. For example we could define age and then use it in controls: local age "age agesq" Macro definitions may include other macros. But you always check your output, right? A Technical Note on Nested Macros Either way, the regression will run without any controls. The same would happen if you type `control' because macro names cannot be abbreviated the way variable names can. If you type regress outcome treatment `contrls', Stata will read regress outcome treatment, because the macro contrls does not exist. So be careful to spell macro names correctly. ![]() Warning: Evaluating a macro that doesn’t exist is not an error it just returns an empty string. This approach also has the advantage that if later on you realize that you should have used log-income rather than income as a control, all you need to do is change the macro definition at the top of your do file, say to read logincome instead of income and all subsequent models will be run with income properly logged (assuming these variables exist). If there’s only one regression to run you haven’t saved anything, but if you have to run several models with different outcomes or treatments, the macro saves work and ensures consistency. Which in this case is exactly equivalent to typing regress outcome treatment age agesq education income. You then type commands such as regress outcome treatment `controls' The smart way is to define a macro local controls age agesq education income You could, of course, type these names in each equation, or you could cut and paste the names, but these alternatives are tedious and error prone. You need to run a bunch of regression equations that include a standard set of control variables, say age, agesq, education, and income. Example: Control Variables in Regression. The text is often enclosed in quotes, but it doesn’t have to be. The first variant, without an equal sign, is used to store arbitrary text of up to ~64k characters (up to a million in Stata SE). (Note the use of an opening backtick or left quote and a closing straight quote.) ![]() You define a local macro using local name text and you evaluate it using `name'. Local macros have names of up to 31 characters and are known only in the current context (the console, a do file, or a program). 5.1 MacrosĪ macro is simply a name associated with some text. Other resources were listed in Section 1 of this tutorial. Nick Cox’s regular columns in the Stata Journal are a wonderful resource for learning about Stata. You may also find useful Chapter 18 in the User’s Guide, referring to the Programming volume and/or the online help as needed. To learn more about programming Stata I recommend Kit Baum’s An Introduction to Stata Programming, now in its second edition, and William Gould’s The Mata Book. Your efforts here will not be wasted, however, because these options are complementary to -not a complete substitute for- classic Stata programming. All of these languages are beyond the scope of this introductory tutorial. In addition, it is possible to write Stata plugins in C. Stata 9 introduced a new and extremely powerful matrix programming language called Mata, and Stata expanded the choice of languages by integrating Python in version 16 and Java in version 17. However, the material covered will help you use Stata more effectively. This is a large subject and all I can hope to do here is provide a few tips that hopefully will spark your interest in further study. I discuss macros and loops, and show how to write your own (simple) programs. This section is a gentle introduction to programming Stata. Introduction Data Management Tables Graphics Programming 5 Programming Stata
0 Comments
Read More
Leave a Reply. |