Programming in STATA
Programming in STATA
Stata: Programming
Daver C. Kahvecioglu
ITS High Performance Computing Group
UNC Chapel Hill
research@unc.edu
Do-files
Rather than typing commands at the keyboard, you can create a disk file containing commands and
instruct Stata to execute the commands stored in that file. Such files are called do-files, since the
command that causes them to be executed is do.
You can use any text editor to create do-files, or you can use the built-in do-file editor by typing
doedit, or by clicking on the do-file editor icon on the menu bar at the top.
By default, if any line in the do-file contains an error, Stata stops immediately and does not attempt
to execute the rest of the commands. If you want Stata to keep going even though something may
be wrong with the do file, add the nostop option:
do file, nostop
1
Last update: April 6, 2004 Stata: Programming Class Notes
c:\stata\wsestata /b do myjob
assuming that Stata-SE is installed in the folder c:\stata and you have a do-file named myjob in the
current folder. When the do-file completes, the Stata icon in the taskbar will flash. You can then
click on it to close Stata. If you want to stop the do-file before it completes, click on the Stata icon
in the taskbar, and Stata will ask you if you want to cancel the job.
If you do not know where Stata is installed, you can right click on the Stata icon on the desktop and
click on Properties. You can also click on Start, then Programs, then right click on Stata to see the
location. For example, in ATN labs we should type
J:\.isis.unc.edu\pc-pkg\stata-80\program\wsestata /b do myjob
Macros:
Macros are the variables of Stata programs. A macro is a string of characters, called the
macroname, that stands for another string of characters, called the macro contents.
There are two types of macros: Local and global. Local macros are private to the program in which
they are defined. They cannot be accessed from outside that program. Global macros, on the other
hand, are public, and are accessible in any program.
sysuse auto
local set1 " weight foreign "
global set1 " weight foreign "
2
Last update: April 6, 2004 Stata: Programming Class Notes
Enclosing local macro names in single quotes exposes what they contain. The content of a global
macro is revealed when we prefix $ to the macroname.
display `set1'
display $set1
display "`set1'"
display "$set1"
display `set1' resolves to display weight foreign. display displays strings and values of scalar
expressions.
display weight foreign displays the most reasonable scalar that you can get out of
weight foreign that is weight[_n] foreign[_n], which is also weight[1]
foreign[1] since current observation starts from observation # 1.
If we enclose the content of a macro in double quotes, (for example, "`set1'" or "$set1")
then content of the macro is nothing but a string.
3
Last update: April 6, 2004 Stata: Programming Class Notes
Let's say you want to write a do-file that gets a data set name (technically, we are passing an
argument, datasetname, to the do-file here) from the user and produces the means of all the
variables in that data set. So, the user will type something like
do dofilename datasetname
and the result will be the summary statistics for the variables in the datasetname.
use `1'
summarize
Or,
args varname
use `varname'
summarize
Programs
When you type something, Stata first checks if it is a built-in command. If it is, Stata executes what
you typed. Otherwise, Stata checks if it's a defined program. If it is, the program is executed.
Otherwise, Stat looks in certain directories (the names of these directories can be seen by typing
sysdir) for a file that has the name you typed and the extension, ado. If the search is not
successful, we get the "unrecognized command" error. In this section we will briefly discuss
programs, which is the second type of object Stata thinks what we typed is. In the next section we
will briefly discuss ado-files, which is last thing Stata thinks what we typed is.
4
Last update: April 6, 2004 Stata: Programming Class Notes
program hello
Now, you will start typing the first line of your program named hello:
1. display "Hello World"
2. end
When you type end on line 2, line 2 becomes the last line of the program and thus program
declaration is ended.
Now program hello is loaded into memory and it can be executed by typing
hello
If you type your program in an editor and save it as a do-file, then you can load it by "do"ing your
do-file. If you want to run your program by "do"ing it, add hello as the last line of your do-file.
Ado-files
To run a program you have to load it first. If you save your program with the .ado extension and
put it in certain directories, you do not have to load it. Stata will treat your program as a Stata
command once it finds it in one of those designated directories.
Some of Stata's own commands are written as ado-files. Rest of Stata's commands are buil-in
commands. You can tell if a command is built-in or not by typing which command.
. which table
C:\PROGRAM FILES\STATA8-SE\ado\base\t\table.ado
*! version 5.3.0 09oct2001
5
Last update: April 6, 2004 Stata: Programming Class Notes
. which tabulate
built-in command: tabulate
You can read the contents of table.ado by opening it in a text editor (they are ASCII text files),
or by typing
You can add your own commands by creating your own ado-files. An ado-file defines a Stata
command, even though there are some Stata commands which are not ado-files.
Stata looks for ado-directories in seven places, which can be categorized in three ways:
I. the official ado-directories, meaning
1. (UPDATES), the official updates directory
2. (BASE), the official base directory
II. your personal ado directories, meaning
3. (SITE), the directory for ado-files your site might have installed,
4. (PLUS), the directory for ado-files you personally might have installed,
5. (PERSONAL), the directory for ado-files you personally might have written, and
6. (OLDPLACE), the directory where Stata users used to save their personally written ado-
files; and
III. the current directory, meaning
7. (.), the ado-files you have written just this instant or for just this project.
. sysdir
STATA: /usr/local/stata8/
UPDATES: /usr/local/stata8/ado/updates/
BASE: /usr/local/stata8/ado/base/
SITE: /usr/local/ado/
PLUS: ~/ado/plus/
PERSONAL: ~/ado/personal/
OLDPLACE: ~/ado/
6
Last update: April 6, 2004 Stata: Programming Class Notes
Stata has a range command that is shipped with it. range generates a numerical range, which is
useful for evaluating and graphing functions. Here is the syntax for it:
Now, type
rangeours 100 1 2
You can access the results of Stata commands after they are executed. In terms of the way their
results are accessed, there are 4 types of commands in Stata:
After running an r-class command, say summarize, type return list to get the list of saved
results such as the mean, variance, maximum, and minimum of the variable. After running the
7
Last update: April 6, 2004 Stata: Programming Class Notes
regress command, which is an e-class command, type ereturn list to get the list of saved
estimation results, such as the number of observations, degrees-of-freedom, r-squared, coefficient
estimates, etc. If you type creturn list, then you get the list of system values such as, today's
date, current time, current directory, current system settings, etc. You can also save your programs'
results. See Section 21.10 of User's Guide for how to do that.
Some Examples
Example 1:
version 8 // This tells Stata the version under which this do-file is written
set more off // Now Stata does not pause every time the screen is full
log using /* unless you specify an explicit address, myjob.log is saved in the current
directory */ myjob, replace text
use http://www.stata-press.com/data/r8/cencus /// the command
continues on the next line/* , clear
log close
8
Last update: April 6, 2004 Stata: Programming Class Notes
Example 2:
local logfilename1 =
upper( word(c(current_date),1) + ///
word(c(current_date),2) + ///
word(c(current_date),3) )
log using `logfilename1', text replace
Example 3:
Let's say we want to create a log file and want to name it as the name of the data set. c(filename)
gives us the name of the data set with the whole path to the data set. We are interested only in the
name of the data set. The following do-file first trims the extension off the name, and then trims the
path all the way up to the name of the data set. Note that we make use of the fact that the upper and
lower cases of "." and "/" are the same.
9
Last update: April 6, 2004 Stata: Programming Class Notes
Example 4:
forvalues repeatedly sets local macro macroname to each element of range and executes the
commands enclosed in braces.
forvalues x = 1/10 {
if mod(`x',2) {
display "`x' is odd"
continue
}
display "`x' is even"
}
foreach repeatedly sets local macro macroname to each element of the list and executes the
commands enclosed in braces. In Example 5 the list is "newlist" (we are creating new variables)
and in Example 6 the list is a "numlist" (we are doing things for each number in the number list).
Example 5:
10
Last update: April 6, 2004 Stata: Programming Class Notes
Example 6:
Example 7:
clear
set obs 100
*Generate 10 uniform random variables named x1, x2, ..., x10.
set seed 12345
forvalues i = 1(1)10 { // equivalently 1/10, or 1 2 to 10
generate x`i' = uniform()
qui cou if x`i' < .1
display " % of x`i' < 1/10 = " round(100*r(N)/_N,.01)
gen x`i'ltdec = x`i' < .1
}
sum x*dec
11
Last update: April 6, 2004 Stata: Programming Class Notes
Example 8:
12
Last update: April 6, 2004 Stata: Programming Class Notes
savas
When you are on sunny, you can convert a SAS dataset (say, filename.sas7bdat) into a Stata data
set, by simply typing:
savas filename.sas7bdat
You can also convert a Stata data set (say, filename.dta) into a SAS data set, by simply typing
savas filename.dta
These are two user-written Stata programs installed on sunny. In other words, they are two user-
written commands that can be run within Stata running on sunny.
usesas allows you to read a SAS data set directly into Stata:
usesas using filename.sas7bdat
savasas allows you save the current data set in Stata's memory as a SAS data set:
savasas using filename.sas7bdat
13
Last update: April 6, 2004 Stata: Programming Class Notes
savas, usesas, and savasas are all written by Dan Blanchette of UNC's Carolina Population
Center. You can install these programs on your personal copies of Stata as well. For more
information please see the following Stata Resources section.
sas2stata
When you are on sunny, there is another simple way you can convert a SAS dataset (say,
filename.sas7bdat) into a Stata data set:
sas2stata filename.sas7bdat
Sas2stata is a Unix utility - written by the RAND Corporation - which converts SAS data sets into
Stata format. For more information in sas2stata on sunny, visit:
http://www.unc.edu/atn/hpc/applications/index.shtml?id=4208
14
Last update: April 6, 2004 Stata: Programming Class Notes
Stata Resources
There are several excellent Stata resources available on the Internet. They include program
databases, discussion forum, and task-specific user web sites. Here I will list only a few of them:
15