![stata if else stata if else](https://www.dummies.com/wp-content/uploads/414885.image0.jpg)
Legend (order(1 "Average User: Video" 2 "Heavy User: Video" 3 "Average User: Text" 4 "Heavy User: Text")) (scatter length_text week if group = 2, xlabel(0 (1) 10) `option4'), /// (scatter length_text week if group = 1, xlabel(0 (1) 10) `option3') /// (scatter length_video week if group = 2, xlabel(0 (1) 10) `option2') /// graph twoway (scatter length_video week if group = 1, xlabel(0 (1) 10) `option1') ///
![stata if else stata if else](https://www.stata.com/features/overview/i/doedit-myd2-ado.png)
local option4 msymbol(th) mcolor(dknavy) clcolor(dknavy) connect(l) local option3 msymbol(o) mcolor(dknavy) clcolor(dknavy) connect(l) local option2 msymbol(th) mcolor(cranberry) clcolor(cranberry) connect(l) local option1 msymbol(o) mcolor(cranberry) clcolor(cranberry) connect(l) To use macros to hold multiple options to make a graph: local condition “if level = 1 & track != 2” local control `control' high_school entrance_exam admission_track In the case of holding long variable lists in a macro, we can extend the macro each time by referring to itself from the last iteration:
![stata if else stata if else](https://i.ytimg.com/vi/KqWAmpb4iyc/hqdefault.jpg)
reg gpa `application' `open_day' `placement' `control' local open_day writing interview participation We can then use the macros in the OLS regressions where application, open_day and placement have all the relevant independent variables and control has all the control variables. To change the contents of a macro, we simply redefine the contents where they are.įirst defines the local macro container and then modifies the contents of container simply by reassigning the variables.īelow we discuss some common scenarios where one can use macros to automate the analyses.īack to top The many uses of macros defining variable listsĪ common use of macros is to hold variable lists for later use.īelow we created a local macro control to hold all control variables, and macros application, open_day and placement to hold variable lists on application, open day performance and placement tests. Make sure you remember all the global macros you created elsewhere. We use a dollar sign to refer to a global macro: $ globalmacro.īe careful with the global macros that are accessible from all do-files and programs. Global macros work across the programs in Stata. Local reason `" "Work on a class assignment/paper" "Use specialized databases (e.g. However, if the strings contain spaces in between, we need to enclose `" "'. Usually we can define macros of strings with or without the quotation marks. Note that the left side is a backtick(`) usually found on the top left corner of the keyboard, and the right side is a apostrophe(').
![stata if else stata if else](https://www.statanordic.com/stata-17/license-and-activation-key-1.png)
This is how you refer to a local macro: `localmacro'. Local macros only work in the current do-file or program. Macros are strings we assign to represent variables, values, texts, commands, statements etc. Gabriel Rossman, Introduction to Stata Programmingīack to top Macros Local and global macros Gen electron_right=(electron=1) if electron<.UCLA: Statistical Consulting Group, Introduction to Stata Programming 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<. if conditions ensure that each respondent gets the right value of edu_cat based on its value of educ. 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):
#Stata if else how to#
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.