triadascripts.blogg.se

Stata if else
Stata if else














This approach also has the advantage that if later 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.

stata if else

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.

stata if else

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 a backtick or left 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). 4.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 the 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 or Java. Stata 9 introduced a new and extremely powerful matrix programming language called Mata, and Stata 16 expanded the choice of languages by integrating Python. 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.

#Stata if else how to#

I discuss macros and loops, and show how to write your own (simple) programs. This section is a gentle introduction to programming Stata. Gen electron_right=(electron=1) if electron<.Introduction Data Management Graphics Programming 4 Programming Stata Gen bigbang_right=(bigbang=1) if bigbang<. Label define edcats 1 "Less than HS" 2 "HS" 3 "Some College" /// Label variable edu_cat "Education Category" Gen edu_cat=1 if educ12 & educ16 & educ12 & educ16 & educ<.

stata if else

if conditions ensure that each respondent gets the right value of edu_cat based on its value of educ.

stata if else

You'll need an initial gen command to create the new variable and handle one of the categories, and a replace command for each of the remaining categories. Let's turn the educ variable ("HIGHEST YEAR OF SCHOOL COMPLETED") into a categorical variable edu_cat, with the categories "Less than High School", "High School", "Some College", "Bachelors", and "Advanced". With replace, an observation that doesn't meet the if condition is left unchanged. With gen, an observation that doesn't meet the if condition will not get a value for the new variable-it will be missing instead. The gen and replace commands will often have if conditions. The something you're setting the variable to will be the result of some math, but it can be really simple math, like a single number. The basic syntax is the same for both commands: Since replace can destroy data, it has no abbreviation. You can change the value of an existing variable using replace. You create a new variable in Stata using the generate command, usually abbreviated gen. Start with the usual setting up (see Doing Your Work Using Do Files): In this article you'll learn how to create new variables and change existing variables.Īssuming you created an SFS folder while reading Managing Stata Files, go to that folder and create a new do file called newvars.do. If you are new to Stata we strongly recommend reading all the articles in the Stata Basics section. This article is part of the Stata for Students series.














Stata if else