Using Stata for Survey Data Analysis
Minot
Page 55
SECTION 11: INTRODUCTION TO PROGRAMMING WITH STATA
This section provides a very quick introduction to the topic of programming with Stata.
We touch on
three topics:
creating and using macros
creating and
using loops
matrix algebra
The purpose here is not to provide a comprehensive description of how to program with Stata, but
rather to give you an idea of the kinds of things that can be done with Stata.
To fully describe Stata
programming would require more space than is available here. In fact, it fills an entire book in the
Stata manuals.
Using macros
Macro assign a set of word or a number to a name. There are two types of macros.
“Global” macros stay in memory until you
leave Stata
“Local” macros exist only with a program or a loop
The syntax is relatively simple:
global gmname = “ expression “
local lmname = “ expression “
To
use these macros later, you must use special symbols to tell Stata they are macros:
$gmname
`lmname‟
One use of the global macro is to store the name of the folder with the data.
global path = “d:\Bhutan\2003 BLSS\Stata”
use “$path\food expenditure.dta”, clear
In addition to
saving you some time, this macro is useful if you share the program with others who
have different names for the folders on their computer.
By using the macro, your colleague can
change the global command once rather than trying to change the path in
every command that opens a
file or saves a file.
Local macros are used (among other places) in loops with the
while command, so we will discuss
them in the next section.
Do'stlaringiz bilan baham: