You can even include macros in macro definitions. So what just happened? An undefined macro will be replaced by nothing. So what Stata saw was. In most cases however, using an undefined macro will lead to syntax errors. If you mistype a macro name, Stata will assume you meant some other, currently undefined macro--this can be a particularly difficult error to debug.
You could use a command like this to increment a counter. Macros are perfectly legal in file names for log files and data sets. For example if you were creating separate data sets by race and sex, you could just define macros for race and sex and then use them in the save command.
If you type. If the save command followed. You could also use macros as a replacement for copy and paste in your text editor: assign a macro to a short piece of code that is repeated in your program and just use type the macro instead of the code. But this generally makes your code much harder to read. For example:. In order to know what's going on, you have to find the most recent definition of macro1.
On the other hand, if used wisely macros can make your code clearer. The key is to use well-named macros to substitute for logical chunks of code. For example, if you had a big list of control variables that you used constantly, you could define the list as a a macro called controls. Then instead of. Or if you repeatedly deal with subsamples of your data, you could define a macro that gave the conditions for that subsample.
For example. You could save a bit of typing by including the if within the macro; clearly it only makes sense when following an if. But you don't want to in order to preserve the readability of your code. In both of these cases, the macros perhaps arguably make your code clearer by hiding the details of the implementation.
A program allows you to define a chunk of code in one place and run it repeatedly. You can also pass in parameters which will be stored as macros, then use those macros in various ways within the program.
A Stata program is just some Stata code with the line program define name at the beginning and the line end at the end. It is a tradition that the first program you write in a new language simply display the message "Hello World" who starts these traditions?
Note that Stata provides the line numbers for you; you will not put them in when you write a program in a do file see the example code at the end. To run your program, type hello. Okay, now you've paid your dues to tradition. More importantly you now understand the mechanics of writing a program. Now any time you want to say "Hello World" all you have to do is type hello. What a time-saver! But even supposing you had a reason to say "Hello World" once, to say it more than once in exactly the same way seems a bit redundant.
You need to add some flexibility to your program, and that's where the macros come in. When running a program, anything typed after the program name will be interpreted as arguments.
Arguments for programs work much like mathematical functions: the program does whatever it does depending on its arguments. You can't have two hello programs at once. You need to get rid of the original by typing:. This can be a minor nuisance if you're running Windows Stata or GUI Stata on Linux, where you can't really be sure what has been going on before your do file is executed. If you try to define a program that's already been defined, your. If you try to drop a program that hasn't been defined for example if you tried program drop hello twice your do file will crash again, with the message.
The solution lies in the capture command. When a command is preceded by capture , any errors it generates are ignored they are captured by capture. So capture program drop hello will get rid of a program called hello if it exists, and do nothing if it doesn't.
Now back to your modified hello program. Try running it by typing hello and see what it does. Now try typing. Let's change our hello program one more time:. If you are an excellent typist you may have missed an important lesson, but the rest of us got it: never try to input a program interactively.
One mistake and you have to drop the program and start all over again. Always define programs in. The logical equivalent of a Stata. You'll probably hear people refer to do files as programs all the time I do it , and don't be confused if someone starts calling Stata a program a subroutine. Most Stata commands are really loops.
Stata carries out the command for the first observation, then the second, and so forth. Take advantage of this looping structure whenever you can, because it is quite fast. But it's not hard to imagine other loops you might want: for example, you might want to execute the same command for five different variables.
Stata allows you to do these too--you just have to write them yourself. The foreach command allows you to create loops that loop over a list of things. As always, make the name informative. You'll use in for generic lists, of for all others. The list type is optional. If it is omitted Stata will interpret what follows as a generic list.
Finally there will be the list to be acted on, and then a left curly bracket. Everything inside the curly brackets will be executed once for each item in the list, and macroname is a local macro that will contain each item in the list in turn.
Let's look at an example:. Note that, as with programs, Stata gives you line numbers when you type a foreach loop interactively but you will not need to type them in do files.
Using in with no list type indicates a generic list. Stata makes no attempt to interpret what follows other than break it up into elements. Thus our loop runs three times, once for each element. However, if for some reason you wish to avoid specifying a default, you could instead code: Code:. Last edited by David Fisher ; 14 Sep , Nick, David thank you both! Best Nigel. Ayrton Dextre. The syntax you define with the syntax command differs from what you later type. With the syntax command, you type square brackets to denote optional arguments.
With few exceptions, you do not type square brackets when you later call a command. Also with the syntax command, you use uppercase letters in options to denote minimal abbreviations. With few exceptions, options are later typed in lowercase letters. We can use this information to compute a statistics, such as, the coefficient of variation that Stata does not provide.
The ereturn list abbr: eret lis is used following estimation commands, such as, regress , anova , logit , sem , etc. Here is an example following regress. The matrix e b contains the parameter estimates while e V has the covariance matrix of the parameter estimates. For example, if you wanted the predicted score for a female with a reading score of 60, you could type the following. Macro variables are a good way to store values for later use.
Stata supports two kinds of macro variables: 1 global macros and 2 local macros. Global macros are saved until Stata is shut down or the macros are cleared while local macros exist only while the do-file or ado-file is being run.
Then they disappear. Macros have a name and a way to refer to the values stored in the macros. Say I wanted to compute the difference in medians for two groups. Here is one way you could do this using local macros.
This only scratches the surface of the utility of macro variables. We will see other examples as we go along. Many programming languages support looping. Stata has several ways of doing loops: foreach , forvalues and while. For the first example we want to create centered variables and squared center variables for five variables. For our second example we want to create a long dataset in which our variables are stacked on top on one another. It is possible to loop throught observations rows but it is usually not necessary.
Here is an example in which we create an index value that is equal to the observation number.. There is no end to the uses for looping.
Every good programming language has the ability to conditionally execute commands. Stata is no exception. It has both if and else to allow you to controls the execution of commands. We will illustate the use of if with an example to runs a series of ttests and reports which ones are statistically significant. Here one way. Here is a much better simpler and faster way to get the same results by subsetting with if as part of list the command.
Bootstrapping is an alternative method for determining standard errors. For standard estimation commands, bootstrapping is just a matter of using the vce boot option. But what if we want bootstrap standard errors for a statistic that Stata does not compute.
0コメント