You are on page 1of 5

IDEC8017-14-01

1
Crawford School of Economics and Government
Introduction to Stata (v11.0
1
)


Q1. How do I get started in Stata?
Log on to the computer in the Crawford labs using your student id and password. Then go to
the Start Menu and select
Start Menu => All Programs => ... =>Stata.
Q2. What are all these windows?
Command: For you to type your command and when you hit enter, the command will be
executed
Results: Results appear in this window when you type commands in the command window
Variables: Lists the names and labels of all the variables in the current data file, including any
you might have created
Review: Keeps record of all your commands
Use the Page Up and Page Down keys to cycle through past commands. If you select a
past command in the Review window, it will appear automatically in the Command
window. You can edit the command before executing it again.
Q3. How can arrange the window layout to suit my taste?
Right-click in the Results Window, in the pop-up menu you will be able to choose your
preferred font and other preferences
Save your preference by going to Stata/Preferences/SavePreferences/Name
Q4. How do I get a data file into Stata?
It depends on the form of the data file. If it is a formatted Stata data file (file name type *.dta)
then you could forget the startup routine and just double click the data file.
If you are already in Stata, go to the menubar and select File => Open..., or alternatively select
the Open File icon on the taskbar, and then navigate to choose the file you want.
Or you can type in the Stata Command window:
use <path>\filename.dta, clear
This assumes you want to type the whole path for Stata to find the file. It may be easier to
move the file into your H: drive first, and then you can omit the path specification. The option
clear removes any earlier data that might be in Statas memory.
There are other commands for opening different types of data files, such as raw text files and
spreadsheets (from Lotus, Excel, etc.).
Q5. How do I see my data in Stata?
If you want a description of the kinds of variables and their labels, type
describe varlist

1
Note that the Stata version available in the Lab is more updated.
IDEC8017-14-01
2
where varlist is a list of variable names (if you leave it blank Stata interprets the list as being
all variables).
If you want statistical summaries including means and standard deviations, type
summarize varlist
Hint: You only need to type enough of a command name to make the command unique. So
you could type summ varlist or even su varlist (but s varlist is not good enough).
If you want to see the numbers themselves, type
list varlist
Caution: If there are lots of observations the result could fill many screens. You can cut in on
a command by using key combination Ctrl-Break. Next time you could look at selected
observations (See Q6 below).
Hint: Another way to see your data is to use the Data Editor, or for more safety the Data
Browser. These bring up the data in a spreadsheet format. They are started by command or
from the menubar or the toolbar.
Q6. How do I transform or create a variable?
The command has the form
generate newvar=expression
where expression is practically any common mathematical expression.
Some operators
Relational
Arithmetic Logical (numeric and string)
-------------------- ------------------ ---------------------
+ addition & and > greater than
- subtraction | or < less than
* multiplication ! not >= > or equal
/ division ~ not <= < or equal
^ power == equal
- negation != not equal
+ string concatenation ~= not equal
A double equal sign (==) is used for equality testing.
The order of evaluation (from first to last) of all operators is
! (or ~), ^, - (negation), /, *, - (subtraction), +, != (or ~=), >, <, <=, >=, ==, &, and |.

Q7. What is the common command syntax in Stata:
command [variable list] [if expression] [in range] [, options]
- A command must include a command name, such as describe, list, browse
ect.
- Do not type the square brackets in a command with some exceptions. These brackets
indicate optional parts of the command line
- If the parts [if expression], [in range], [, options] are included in a command, they
must start with if, in, or , , respectively.
IDEC8017-14-01
3
- Variable list is a list of variables separated by (a) blanks
- The range may be:
1 the first observation
2 the third observation
-1 the last observation
-3 the third observation from the last
1/5 observations from 1 to 5
If in range is not specified, the range will be 1/-1 (all observations) by default
- In if expressions, we can use logical operators. Please see the full list of operators
below.
- options may vary across commands.
Q8. How do I select a subset of observations for transformation or analysis?
If you want to select on the observation index, use the in option. The command
summ myvar in 1/25
does the calculation on just the first 25 observations. On the other hand
gen myvar=hisvar if hisvar<6
or list myvar if hisvar==6
will do the calculations only for the cases in the data where the condition is met. The
relational symbols are >, <, >=, <= and ==. Note the double equals sign for a test of equality;
a single equals sign is the assignment operator used in the generate command.
Q9. What do I do if I get a screen full of output and the message more?
You can choose to see the rest of the output screen-at-a-time by pressing the space bar, or you
can go line-at-a-time by pressing Enter. If you dont want any more of the current output,
press Ctrl-Break (In Mac, Command-.).
Q10. How can I know how a variable was coded?
Suppose you want to know how a variable is coded (or labelled), type
codebook varlist
Q11. How do I execute a batch of commands without typing them in every time?
A batch file of input instructions in Stata is called a .do file and the tool you want is the
Do-file Editor. This can be started from either the menubar or the toolbar. The menubar of the
Do-file Editor has commands to open existing files of commands, to save newly created or
modified files, and to execute either the whole file of commands or just a selected portion of
it. The command do myfile.do will also execute the file of Stata commands.
Q11. How can I save my results to study later or to include in my assignment?
You can copy-and-paste from the Stata Results window for a small amount of recent
calculations. For larger runs of output you need to write the results to a log file. Again you
IDEC8017-14-01
4
will find the commands in the menubar and the toolbar. In a .do file you can turn logging on
and off with the commands
log using h:\tut1.log and log close
There are two types of log files, one which is just plain text and useful for copying into other
programs (called .log), another which is more highly decorated output but not much use in
other programs (called .smcl).
If the log file already exists, the first command above will have to be modified to be either
log using h:\tut1.log, append
or log using h:\tut1.log, replace
Q12. How can I make notes to myself (comments) in a .do file?
Any line that begins with an asterisk * is treated as a comment. Stata only echos the line and
then ignores it.
Q13. Some lines in my .do file are pretty long. How can I break them?
Stata normally takes the carriage return at end of a line as the indicator that a command is
complete and ready to be executed. One trick is to define a new end of command character.
#delimit ;
list onevar
twovar
threevar;
#delimit cr
This tells Stata to keep reading until it reaches a semicolon before the command is complete
and ready to be executed. All extra spaces and carriage returns are ignored. The feature can be
turned off by resetting the delimiter character to the carriage return. The above example has
the same effect as
list onevar twovar threevar
Hint: The same trick lets you put several short instructions on the same line of a .do file.
Q14. How do I find out more about Stata commands?
Many commands can be specified from the menus Data, Graphics and Statistics.
There is a very extensive online help system. It distinguishes between help on a command
name (menu: Help => Stata Command...) and help on a topic (menu: Help => Search...).
It is often much more efficient to search in the internet to know what to look for in Stata
before resorting to the Help Resources in Stata.
For example: google stata descriptive statistis beginners, on the first page, two very good
websites are that we highly recommend you to visit
http://data.princeton.edu/stata/
http://www.princeton.edu/~otorres/Excel/excelstata.htm
Resources for learning Stata
http://www.stata.com/links/resources-for-learning-stata/
A must-visit website on visual overview for creating graphs in Stata
http://www.stata.com/support/faqs/graphics/gph/stata-graphs/
IDEC8017-14-01
5

Example .do file (assumes you have the data file wages1.dta in a folder h:\tut1)
*Change the directory. Close any existing log files and start a new log.
cd h:\tut1
capture log close
log using tut1.log, replace

*Load the data and have a look at it.
use wages1.dta, clear
desc
list wage male in 1/10

*Do some calculations and make some graphs.
summ wage if male==0
summ wage if male==1
plot wage school
twoway (scatter wage school)
regress wage male
regress wage male school

*Close the log file.
log close

*****************

You might also like