STATA BOOK
STATA BOOK
®
FOR WINDOWS
RELEASE 18
Published by Stata Press, 4905 Lakeway Drive, College Station, Texas 77845
ISBN-10: 1-59718-383-0
ISBN-13: 978-1-59718-383-3
This manual is protected by copyright. All rights are reserved. No part of this manual may be reproduced, stored in a
retrieval system, or transcribed, in any form or by any means—electronic, mechanical, photocopy, recording, or other-
wise—without the prior written permission of StataCorp LLC unless permitted subject to the terms and conditions of
a license granted to you by StataCorp LLC to use the software and documentation. No license, express or implied, by
estoppel or otherwise, to any intellectual property rights is granted by this document.
StataCorp provides this manual “as is” without warranty of any kind, either expressed or implied, including, but not lim-
ited to, the implied warranties of merchantability and fitness for a particular purpose. StataCorp may make improvements
and/or changes in the product(s) and the program(s) described in this manual at any time and without notice.
The software described in this manual is furnished under a license agreement or nondisclosure agreement. The software
may be copied only in accordance with the terms of the agreement. It is against the law to copy the software onto DVD,
CD, disk, diskette, tape, or any other medium for any purpose other than backup or archival purposes.
The automobile dataset appearing on the accompanying media is Copyright © 1979 by Consumers Union of U.S., Inc.,
Yonkers, NY 10703-1057 and is reproduced by permission from CONSUMER REPORTS, April 1979.
Certain icons are licensed from Axialis SA. They remain the property of Axialis SA and may not be reproduced or
distributed.
Stata, , Stata Press, Mata, , and NetCourse are registered trademarks of StataCorp LLC.
Stata and Stata Press are registered trademarks with the World Intellectual Property Organization of the United Nations.
StataNow and NetCourseNow are trademarks of StataCorp LLC.
Other brand and product names are registered trademarks or trademarks of their respective companies.
For copyright information about the software, type help copyright within Stata.
www.stata.com
Contents
i
Cross-referencing the documentation
When reading this manual, you will find references to other Stata manuals, for example,
[U] 27 Overview of Stata estimation commands; [R] regress; and [D] reshape. The first example
is a reference to chapter 27, Overview of Stata estimation commands, in the User’s Guide; the second
is a reference to the regress entry in the Base Reference Manual; and the third is a reference to the
reshape entry in the Data Management Reference Manual.
All the manuals in the Stata Documentation have a shorthand notation:
ii
About this manual
This manual discusses Stata for Windows○R . Stata for Mac○R users should see Getting Started with
Stata for Mac; Stata for Unix○R users should see Getting Started with Stata for Unix. This manual is
intended both for people who are completely new to Stata and for experienced Stata users new to Stata
for Windows. Previous Stata users will also find it helpful as a tutorial on some new features in Stata for
Windows.
Following the numbered chapters are three appendixes with information specific to Stata for Windows.
We provide several types of technical support to registered Stata users. [GSW] 4 Getting help de-
scribes the resources available to help you learn about Stata’s commands and features. One of these
resources is the Stata website (https://www.stata.com), where you will find answers to frequently asked
questions (FAQs) as well as other useful information. If you still have questions after looking at the
Stata website and the other resources described in [GSW] 19 Updating and extending Stata—Internet
functionality, you can contact us as described in [U] 3.8 Technical support.
iii
1 Introducing Stata—sample session
Introducing Stata
This chapter will run through a sample work session, introducing you to a few of the basic tasks
that can be done in Stata, such as opening a dataset, investigating the contents of the dataset, using some
descriptive statistics, making some graphs, and doing a simple regression analysis. As you would expect,
we will only brush the surface of many of these topics. This approach should give you a sample of what
Stata can do and how Stata works. There will be brief explanations along the way, with references to
chapters later in this book as well as to the system help and other Stata manuals. We will run through
the session by using both menus and dialogs and Stata’s commands so that you can become familiar
with them both. If you see that your menus and dialogs are not in English, we recommend that you
(temporarily) change the locale used by Stata to English, so that you can work along with the examples.
See [P] set locale ui for how to do this.
Take a seat at your computer, put on some good music, and work along with the book.
Sample session
The dataset that we will use for this session is a set of data about vintage 1978 automobiles sold in
the United States.
To follow along by pointing and clicking, note that the menu items are given by Menu > Menu item
> Submenu item > etc. To follow along by using the Command window, type the commands that follow
a dot (.) in the boxed listings below into the small window labeled Command. When there is something
to note about the structure of a command, it will be pointed out as a “Syntax note”.
Start by loading the automobile dataset, which is included with Stata. Use the menus to do this:
1. Select File > Example datasets....
2. Click on Example datasets installed with Stata.
3. Click on use for auto.dta.
The result of this command is fourfold:
• The following output appears in the large Results window:
. sysuse auto
(1978 automobile data)
The output consists of a command and its result. The command, sysuse auto.dta, is bold and
follows the dot (.). The result, (1978 automobile data), is in the standard face here and is a
brief description of the dataset.
Note: If a command intrigues you, you can type help commandname in the Command window to
find help. If you want to explore at any time, Help > Search... can be informative.
• The same command, sysuse auto.dta, appears in the tall History window to the left. The History
window keeps track of all commands Stata has run, successful and unsuccessful. The commands
can then easily be rerun. See [GSW] 2 The Stata user interface for more information.
1
[ GSW ] 1 Introducing Stata—sample session 2
• A series of variables appears in the small Variables window to the upper right.
• Some information about make, the first variable in the dataset, appears in the small Properties
window to the lower right.
You could have opened the dataset by typing sysuse auto in the Command window and pressing
Enter. Try this now. sysuse is a command that loads (uses) example (system) datasets. As you will see
during this session, Stata commands are often simple enough that it is faster to use them directly. This
will be especially true once you become familiar with the commands you use the most in your daily use
of Stata.
Syntax note: In the above example, sysuse is the Stata command, whereas auto is the name of a
Stata data file.
The data are displayed in multiple colors—at first glance, it appears that the variables listed in black are
numeric, whereas those that are in colors are text. This is worth investigating. Click on a cell under the
make variable: the input box at the top displays the make of the car. Scroll to the right until you see the
foreign variable. Click on one of its cells. Although the cell may display “Domestic”, the input box
displays a 0. This shows that Stata can store categorical data as numbers but display human-readable
text. This is done by what Stata calls value labels. Finally, under the rep78 variable, which looks to be
numeric, there are some cells containing just a dot (.). The dots correspond to missing values.
Looking at the data in this fashion, though comfortable, lends little information about the dataset. It
would be useful for us to get more details about what the data are and how the data are stored. Close the
Data Editor by clicking on its close button.
We can see the structure of the dataset by describing its contents. This can be done either by going
to Data > Describe data > Describe data in memory or in a file in the menus and clicking on OK
or by typing describe in the Command window and pressing Enter. Regardless of which method you
choose, you will get the same result:
. describe
Contains data from C:\Program Files\Stata18/ado/base/a/auto.dta
Observations: 74 1978 automobile data
Variables: 12 13 Apr 2022 17:45
(_dta has notes)
At the top of the listing, some information is given about the dataset, such as where it is stored on
disk and when the dataset was last saved. The bold 1978 automobile data is the short description that
appeared when the dataset was opened and is referred to as a data label by Stata. The phrase dta has
notes informs us that there are notes attached to the dataset. We can see what notes there are by typing
notes in the Command window:
. notes
_dta:
1. From Consumer Reports with permission
[ GSW ] 1 Introducing Stata—sample session 4
make 0
price 74 6165.257 2949.496 3291 15906
mpg 74 21.2973 5.785503 12 41
rep78 69 3.405797 .9899323 1 5
headroom 74 2.993243 .8459948 1.5 5
From this simple summary, we can learn a bit about the data. First of all, the prices are nothing like
today’s car prices—of course, these cars are now antiques. We can see that the gas mileages are not
particularly good. Automobile aficionados can get a feel for other esoteric characteristics.
[ GSW ] 1 Introducing Stata—sample session 5
The first line of the output tells us the variable name (make) and the variable label (Make and model).
The variable is stored as a string (which is another way of saying “text”) with a maximum length of
18 characters, though a size of only 17 characters would be enough. All the values are unique, so if
need be, make could be used as an identifier for the observations—something that is often useful when
putting together data from multiple sources or when trying to weed out errors from the dataset. There
are no missing values, but there are blanks within the makes. This latter fact could be useful if we were
expecting make to be a one-word string variable.
Syntax note: Telling the codebook command to run on the make variable is an example of using a
varlist in Stata’s syntax.
[ GSW ] 1 Introducing Stata—sample session 6
Looking at the foreign variable can teach us about value labels. We would like to look at the code-
book output for this variable, and on the basis of our latest experience, it would be easy to type codebook
foreign into the Command window (from here on, we will not explicitly say to press the Enter key) to
get the following output:
. codebook foreign
We can glean that foreign is an indicator variable because its only values are 0 and 1. The variable
has a value label that displays Domestic instead of 0 and Foreign instead of 1. There are two advantages
of storing the data in this form:
• Storing the variable as a byte takes less memory because each observation uses 1 byte instead of the
8 bytes needed to store “Domestic”. This is important in large datasets. See [U] 12.2.2 Numeric
storage types.
• As an indicator variable, it is easy to incorporate into statistical models. See [U] 26 Working with
categorical data and factor variables.
Finally, we can learn a little about a poorly labeled variable with missing values by looking at the
rep78 variable. Typing codebook rep78 into the Command window yields
. codebook rep78
rep78 appears to be a categorical variable, but because of lack of documentation, we do not know what
the numbers mean. (To see how we would label the values, see Changing data in [GSW] 6 Using the Data
Editor and see [GSW] 9 Labeling data.) This variable has five missing values, meaning that there are
[ GSW ] 1 Introducing Stata—sample session 7
five observations for which the repair record is not recorded. We could use the Data Editor to investigate
these five observations, but we will do this by using the Command window only because doing so is
much simpler.
The command equivalent to clicking on the Data Editor (Browse) button is browse. We would like
to browse only those observations for which rep78 is missing, so we could type
. browse if missing(rep78)
From this, we see that the . entries are indeed missing values. The . is the default numerical missing
value; Stata also allows .a, . . . , .z as user missing values, but we do not have any in our dataset. See
[U] 12.2.1 Missing values. Close the Data Editor after you are satisfied with this statement.
Syntax note: Using the if qualifier above is what allowed us to look at a subset of the observations.
Looking through the data lends no clues about why these particular data are missing. We decide to
check the source of the data to see if the missing values were originally missing or if they were omitted
in error. Listing the makes of the cars whose repair records are missing will be all we need because we
saw earlier that the values of make are unique. This can be done with the menus and a dialog:
1. Select Data > Describe data > List data.
2. Click on the drop triangle to the right of the Variables field to show the variable names.
3. Click on make to enter it into the Variables field.
4. Click on the by/if/in tab in the dialog.
5. Type missing(rep78) into the If: (expression) box.
6. Click on Submit. Stata executes the proper command but the dialog remains open. Submit is
useful when experimenting, exploring, or building complex commands. We will primarily use
Submit in the examples. You may click on OK in its place if you like, and it will close the dialog
box.
[ GSW ] 1 Introducing Stata—sample session 8
The same ends could be achieved by typing list make if missing(rep78) in the Command window.
The latter is easier once you know that the command list is used for listing observations. In any case,
here is the output:
. list make if missing(rep78)
make
3. AMC Spirit
7. Buick Opel
45. Plym. Sapporo
51. Pont. Phoenix
64. Peugeot 604
At this point, we should find the original reference to see if the data were truly missing or if they could
be resurrected. See [GSW] 10 Listing data and basic command syntax for more information about all
that can be done with the list command.
Syntax note: This command uses two new concepts for Stata commands—the if qualifier and the
missing() function. The if qualifier restricts the observations on which the command runs to only
those observations for which the expression is true. See [U] 11.1.3 if exp. The missing() function tests
each observation to see if it contains a missing value. See [FN] Programming functions.
Now that we have a good idea about the underlying dataset, we can investigate the data themselves.
Descriptive statistics
We saw above that the summarize command gave brief summary statistics about all the variables.
Suppose now that we became interested in the prices while summarizing the data because they seemed
fantastically low (it was 1978, after all). To get an in-depth look at the price variable, we can use the
menus and a dialog:
1. Select Statistics > Summaries, tables, and tests > Summary and descriptive statistics > Sum-
mary statistics.
2. Enter or select price in the Variables field.
3. Select Display additional statistics.
4. Click on Submit.
Syntax note: As can be seen from the Results window, typing summarize price, detail will get the
same result. The portion after the comma contains options for Stata commands; hence, detail is an
example of an option.
[ GSW ] 1 Introducing Stata—sample session 9
. summarize price, detail
Price
Percentiles Smallest
1% 3291 3291
5% 3748 3299
10% 3895 3667 Obs 74
25% 4195 3748 Sum of wgt. 74
50% 5006.5 Mean 6165.257
Largest Std. dev. 2949.496
75% 6342 13466
90% 11385 13594 Variance 8699526
95% 13466 14500 Skewness 1.653434
99% 15906 15906 Kurtosis 4.819188
From the output, we can see that the median price of the cars in the dataset is only $5,006.50! We can
also see that the four most expensive cars are all priced between $13,400 and $16,000. If we wished to
browse the most expensive cars (and gain some experience with features of the Data Editor), we could
start by clicking on the Data Editor (Browse) button, .
Once the Data Editor is open, we can click on the Filter observations button, , to bring up the
Filter observations dialog. We can look at the expensive cars by putting price > 13000 in the Filter by
expression field:
[ GSW ] 1 Introducing Stata—sample session 10
Pressing the Apply filter button filters the data, and we can see that the expensive cars are two Cadil-
lacs and two Lincolns, which were not designed for gas mileage:
We now decide to turn our attention to foreign cars and repairs because as we glanced through the data,
it appeared that the foreign cars had better repair records. (We do not know exactly what the categories
1, 2, 3, 4, and 5 mean, but we know the Chevy Monza was known for breaking down.) Let’s start by
looking at the proportion of foreign cars in the dataset along with the proportion of cars with each type of
repair record. We can do this with one-way tables. The table for foreign cars can be done with menus
and a dialog starting with Statistics > Summaries, tables, and tests > Frequency tables > One-way
table and then choosing the variable foreign in the Categorical variable field. Clicking on Submit
yields
. tabulate foreign
Car origin Freq. Percent Cum.
Total 74 100.00
We see that roughly 70% of the cars in the dataset are domestic, whereas 30% are foreign. The value
labels are used to make the table so that the output is nicely readable.
[ GSW ] 1 Introducing Stata—sample session 11
Syntax note: We also see that this one-way table could be made by using the tabulate command
together with one variable, foreign. Making a one-way table for the repair records is simple—it will
be simpler if done with the Command window. Typing tabulate rep78 yields
. tabulate rep78
Repair
record 1978 Freq. Percent Cum.
1 2 2.90 2.90
2 8 11.59 14.49
3 30 43.48 57.97
4 18 26.09 84.06
5 11 15.94 100.00
Total 69 100.00
We can see that most cars have repair records of 3 and above, though the lack of value labels makes
us unsure what a “3” means. Take our word for it that 1 means a poor repair record and 5 means a good
repair record. The five missing values are indirectly evident because the total number of observations
listed is 69 rather than 74.
These two one-way tables do not help us compare the repair records of foreign and domestic cars. A
two-way table would help greatly, which we can get by using the menus and a dialog:
1. Select Statistics > Summaries, tables, and tests > Frequency tables > Two-way table with
measures of association.
2. Choose rep78 as the Row variable.
3. Choose foreign as the Column variable.
4. It would be nice to have the percentages within the foreign variable, so check the Within-row
relative frequencies checkbox.
5. Click on Submit.
[ GSW ] 1 Introducing Stata—sample session 12
Key
frequency
row percentage
Repair
record Car origin
1978 Domestic Foreign Total
1 2 0 2
100.00 0.00 100.00
2 8 0 8
100.00 0.00 100.00
3 27 3 30
90.00 10.00 100.00
4 9 9 18
50.00 50.00 100.00
5 2 9 11
18.18 81.82 100.00
Total 48 21 69
69.57 30.43 100.00
The output indicates that foreign cars are generally much better than domestic cars when it comes to
repairs. If you like, you could repeat the previous dialog and try some of the hypothesis tests available
from the dialog. We will abstain.
Syntax note: We see that typing the command tabulate rep78 foreign, row would have given us
the same table. Thus using tabulate with two variables yields a two-way table. It makes sense that
row is an option—we went out of our way to check it in the dialog. Using the row option allows us to
change the behavior of the tabulate command from its default.
[ GSW ] 1 Introducing Stata—sample session 13
Continuing our exploratory tour of the data, we would like to compare gas mileages between foreign
and domestic cars, starting by looking at the summary statistics for each group by itself. A direct way to
do this would be to use if qualifiers to summarize mpg for each of the two values of foreign separately:
. summarize mpg if foreign==0
Variable Obs Mean Std. dev. Min Max
It appears that foreign cars get somewhat better gas mileage—we will test this soon.
Syntax note: We needed to use a double equal sign (==) for testing equality. The double equal sign
could be familiar to you if you have programmed before. If it is unfamiliar, be aware that it is a common
source of errors when initially using Stata. Thinking of equality as “exactly equal” can cut down on
typing errors.
There are two other methods that we could have used to produce these summary statistics. These
methods are worth knowing because they are less error-prone. The first method duplicates the concept
of what we just did by exploiting Stata’s ability to run a command on each of a series of nonoverlapping
subsets of the dataset. To use the menus and a dialog, do the following:
1. Select Statistics > Summaries, tables, and tests > Summary and descriptive statistics > Sum-
mary statistics and click on the Reset button, .
2. Select mpg in the Variables field.
3. Select the Standard display option (if it is not already selected).
4. Click on the by/if/in tab.
5. Check the Repeat command by groups checkbox.
6. Select or type foreign in the Variables that define groups field.
7. Submit the command.
[ GSW ] 1 Introducing Stata—sample session 14
You can see that the results match those from above. They have a better appearance than the two com-
mands above because the value labels Domestic and Foreign are used rather than the numerical values.
The method is more appealing because the results were produced without needing to know the possible
values of the grouping variable ahead of time.
. by foreign, sort: summarize mpg
Syntax note: There is something different about the equivalent command that appears above: it con-
tains a prefix command called a by prefix. The by prefix has its own option, namely, sort, to ensure
that like members are adjacent to each other before being summarized. The by prefix command is im-
portant for understanding data manipulation and working with subpopulations within Stata. Make good
note of this example, and consult [U] 11.1.2 by varlist: and [U] 13.7 Explicit subscripting for more
information. Stata has other prefix commands for specialized treatment of commands, as explained in
[U] 11.1.10 Prefix commands.
The third method for tabulating the differences in gas mileage across the cars’origins involves thinking
about the structure of desired output. We need a one-way table of automobile types (foreign versus
domestic) within which we see information about gas mileages. Looking through the menus yields the
menu item Statistics > Summaries, tables, and tests > Other tables > Table of means, std. dev., and
frequencies. Selecting this, entering foreign for Variable 1 and mpg for the Summarize variable, and
submitting the command yields a nice table:
. tabulate foreign, summarize(mpg)
Summary of Mileage (mpg)
Car origin Mean Std. dev. Freq.
Group Obs Mean Std. err. Std. dev. [95% conf. interval]
From this, we could conclude that the mean gas mileage for foreign cars is different from that of
domestic cars (though we really ought to have wanted to test this before snooping through the data). We
can also conclude that the command, ttest mpg, by(foreign), is easy enough to remember. Feel free
to experiment with unequal variances, various approximations to the number of degrees of freedom, and
the like.
Syntax note: The by() option used here is not the same as the by prefix command used earlier.
Although it has a similar conceptual meaning, its usage is different because it is a particular option for
the ttest command.
[ GSW ] 1 Introducing Stata—sample session 16
mpg 1.0000
weight -0.8072 1.0000
The equivalent command for this is natural: correlate mpg weight. There is a negative correlation,
which is not surprising because heavier cars should be harder to push about.
We could see how the correlation compares for foreign and domestic cars by using our knowledge of
the by prefix. We can reuse the correlate dialog or use the menus as before if the dialog is closed. Click
on the by/if/in tab, check the Repeat command by groups checkbox, and enter the foreign variable
to define the groups. As done on page 3, a simple by foreign, sort: prefix in front of our previous
command would work, too:
. by foreign, sort: correlate mpg weight
mpg 1.0000
weight -0.8759 1.0000
mpg 1.0000
weight -0.6829 1.0000
We see from this that the correlation is not as strong among the foreign cars.
[ GSW ] 1 Introducing Stata—sample session 17
Syntax note: Although we used the correlate command to look at the correlation of two variables,
Stata can make correlation matrices for an arbitrary number of variables:
. correlate mpg weight length turn displacement
(obs=74)
mpg weight length turn displa~t
mpg 1.0000
weight -0.8072 1.0000
length -0.7958 0.9460 1.0000
turn -0.7192 0.8574 0.8643 1.0000
displacement -0.7056 0.8949 0.8351 0.7768 1.0000
This can be useful, for example, when investigating collinearity among predictor variables.
Graphing data
We have found several things in our investigations so far: We know that the average MPG of domestic
and foreign cars differs. We have learned that domestic and foreign cars differ in other ways as well,
such as in frequency-of-repair record. We found a negative correlation between MPG and weight—as we
would expect—but the correlation appears stronger for domestic cars.
We would now like to examine, with an eye toward modeling, the relationship between MPG and
weight, starting with a graph. We can start with a scatterplot of mpg against weight. The command for
this is simple: scatter mpg weight. Using the menus requires a few steps because the graphs in Stata
may be customized heavily.
1. Select Graphics > Twoway graph (scatter, line, etc.).
2. Click on the Create... button.
3. Select the Basic plots radio button (if it is not already selected).
4. Select Scatter as the basic plot type (if it is not already selected).
5. Select mpg as the Y variable and weight as the X variable.
6. Click on the Submit button.
The Results window shows the command that was issued from the menu:
. twoway (scatter mpg weight)
[ GSW ] 1 Introducing Stata—sample session 18
The command issued when the dialog was submitted is a bit more complex than the command sug-
gested above. There is good reason for this: the more complex structure allows combining and overlaying
graphs, as we will soon see. In any case, the graph that appears is
.JMFBHF NQH
8FJHIU MCT
We see the negative correlation in the graph, though the relationship appears to be nonlinear.
Note: When you draw a graph, the Graph window appears, probably covering up your Results win-
dow. Click on the main Stata window to get the Results window back on top. Want to see the graph
again? Click on the Graph button, . See The Graph button in [GSW] 14 Graphing data for more
information about the Graph button.
We would now like to see how the different correlations for foreign and domestic cars are manifested
in scatterplots. It would be nice to see a scatterplot for each type of car, along with a scatterplot for all
the data.
Syntax note: Because we are looking at subgroups, this looks as if it is a job for the by prefix. Let’s
see if this is what we really should use.
Start as before:
1. Select Graphics > Twoway graph (scatter, line, etc.) from the menus.
2. If the Plot 1 dialog is still visible, click on the Accept button and skip to step 4.
3. Go through the process on the previous page to create the graph.
4. Click on the By tab of the twoway - Twoway graphs dialog.
5. Check the Draw subgraphs for unique values of variables checkbox.
6. Enter foreign in the Variables field.
7. Check the Add a graph with totals checkbox.
8. Click on the Submit button.
[ GSW ] 1 Introducing Stata—sample session 19
%PNFTUJD 'PSFJHO
.JMFBHF NQH
5PUBM
8FJHIU MCT
(SBQITCZ$BSPSJHJO
The graphs show that the relationship is nonlinear for both origins of cars.
Syntax note: To make the graphs for the combined subgroups, we ended up using a by() option,
not a by prefix. If we had used a by prefix, separate graphs would have been generated instead of the
combined graph created by the by() option.
foreign is already an indicator (0/1) variable, but we need to create the weight-squared variable. This
can be done with the menus, but here using the command line is simpler. Type
. generate wtsq = weight^2
[ GSW ] 1 Introducing Stata—sample session 20
Now that we have all the variables we need, we can run a linear regression. We will use the menus and
see that the command is also simple. To use the menus, select Statistics > Linear models and related >
Linear regression. In the resulting dialog, choose mpg as the Dependent variable and weight, wtsq, and
foreign as the Independent variables. Submit the command. Here is the equivalent simple regress
command and the resulting analysis-of-variance table.
. regress mpg weight wtsq foreign
Source SS df MS Number of obs = 74
F(3, 70) = 52.25
Model 1689.15372 3 563.05124 Prob > F = 0.0000
Residual 754.30574 70 10.7757963 R-squared = 0.6913
Adj R-squared = 0.6781
Total 2443.45946 73 33.4720474 Root MSE = 3.2827
The results look encouraging, so we will plot the predicted values on top of the scatterplots for each
of the origins of cars. To do this, we need the predicted, or fitted, values. This can be done with the
menus, but doing it in the Command window is simple enough. We will create a new variable, mpghat,
to hold the predicted MPG for each car. Type
. predict mpghat
(option xb assumed; fitted values)
The output from this command is simply a notification. Go over to the Variables window and scroll
to the bottom to confirm that there is now an mpghat variable. If you were to try this command when
mpghat already existed, Stata would refuse to overwrite your data:
. predict mpghat
variable mpghat already defined
r(110);
The predict command, when used after a regression, is called a postestimation command. As spec-
ified, it creates a new variable called mpghat equal to
For careful model fitting, there are several features available to you after estimation—one is calcu-
lating predicted values. Be sure to read [U] 20 Estimation and postestimation commands.
[ GSW ] 1 Introducing Stata—sample session 21
We can now graph the data and the predicted curve to evaluate separately the fit on the foreign and
domestic data to determine if our shift parameter is adequate. We can draw both graphs together. Using
the menus and a dialog, do the following:
1. Select Graphics > Twoway graph (scatter, line, etc.) from the menus.
2. If there are any plots listed, click on the Reset button, , to clear the dialog box.
3. Create the graph for mpg versus weight:
a. Click on the Create... button.
b. Be sure that Basic plots and Scatter are selected.
c. Select mpg as the Y variable and weight as the X variable.
d. Click on Accept.
4. Create the graph showing mpghat versus weight:
a. Click on the Create... button.
b. Select Basic plots and Line.
c. Select mpghat as the Y variable and weight as the X variable.
d. Check the Sort on x variable box. Doing so ensures that the lines connect from smallest to
largest weight values, instead of the order in which the data happen to be.
e. Click on Accept.
5. Show two plots, one each for domestic and foreign cars, on the same graph:
a. Click on the By tab.
b. Check the Draw subgraphs for unique values of variables checkbox.
c. Enter foreign in the Variables field.
6. Click on the Submit button.
Here are the resulting command and graph:
. twoway (scatter mpg weight) (line mpghat weight, sort), by(foreign)
%PNFTUJD 'PSFJHO
.JMFBHF NQH
'JUUFEWBMVFT
8FJHIU MCT
(SBQITCZ$BSPSJHJO
[ GSW ] 1 Introducing Stata—sample session 22
Here we can see the reason for enclosing the separate scatter and line commands in parentheses:
they can thereby be overlaid by submitting them together. The fit of the plots looks good and is cause
for initial excitement. So much excitement, in fact, that we decide to print the graph and show it to
an engineering friend. We print the graph, being careful to print the graph (and not all our results), by
choosing File > Print... from the Graph window menu bar.
When we show our graph to our engineering friend, she is concerned. “No,” she says. “It should
take twice as much energy to move 2,000 pounds 1 mile compared with moving 1,000 pounds the same
distance: therefore, it should consume twice as much gasoline. Miles per gallon is not quadratic in
weight; gallons per mile is a linear function of weight. Don’t you remember any physics?”
We try out what she says. We need to generate an energy-per-distance variable and make a scatter-
plot. Here are the commands that we would need—note their similarity to commands issued earlier in
the session. There is one new command, the label variable command, which allows us to give the
gpm100m variable a variable label so that the graph is labeled nicely.
. generate gp100m = 100/mpg
. label variable gp100m "Gallons per 100 miles"
. twoway (scatter gp100m weight), by(foreign, total)
%PNFTUJD 'PSFJHO
(BMMPOTQFSNJMFT
5PUBM
8FJHIU MCT
(SBQITCZ$BSPSJHJO
[ GSW ] 1 Introducing Stata—sample session 23
Sadly satisfied that the engineer is indeed correct, we rerun the regression:
. regress gp100m weight foreign
Source SS df MS Number of obs = 74
F(2, 71) = 113.97
Model 91.1761694 2 45.5880847 Prob > F = 0.0000
Residual 28.4000913 71 .400001287 R-squared = 0.7625
Adj R-squared = 0.7558
Total 119.576261 73 1.63803097 Root MSE = .63246
We find that foreign cars had better gas mileage than domestic cars in 1978 because they were so light.
According to our model, a foreign car with the same weight as a domestic car would use an additional
5/8 gallon (or 5 pints) of gasoline per 100 miles driven. With this conclusion, we are satisfied with our
analysis.
Here is an example of reading the syntax diagram that uses the summarize command from earlier in
this chapter. The syntax diagram for summarize is typical:
Video example
What’s it like—Getting started in Stata
Conclusion
This chapter introduced you to Stata’s capabilities. You should now read and work through the rest
of this manual. Once you are done here, you can read the User’s Guide.
2 The Stata user interface
The windows
This chapter introduces the core of Stata’s interface: its main windows, its toolbar, its menus, and its
dialogs.
Past commands appear here Results are displayed here Variable list appears here Data properties appear here
Current working directory Commands are typed here Current log status Command log status
appears here appears here appears here
The five main windows are the History, Results, Command, Variables, and Properties windows. Ex-
cept for the Results window, each window has its name in its title bar. These five windows are typically
in use the whole time Stata is open. There are other, more specialized windows such as the Viewer, Data
Editor, Variables Manager, Do-file Editor, Graph, and Graph Editor windows—these are discussed later
in this manual.
To open any window or to reveal a window hidden by other windows, select the window from the
Window menu, or select the proper item from the toolbar. You can also use Ctrl+Tab to cycle through
all open windows inside the main Stata window or Alt+Tab to cycle through all open windows (Stata
and other) if you want to change windows from the keyboard. Many of Stata’s windows have func-
tionality that can be accessed by clicking on the right mouse button (right-clicking) within the window.
Right-clicking displays a contextual menu that, depending on the window, allows you to copy text, set
the preferences for the window, or print the contents of the window. When you copy text or print, we
recommend that you always right-click on the window rather than use the menu bar or toolbar so that
you can be sure of where and what you are copying or printing.
25
[ GSW ] 2 The Stata user interface 26
The toolbar
This is the toolbar:
The toolbar contains buttons that provide quick access to Stata’s more commonly used features. If
you forget what a button does, hold the mouse pointer over the button for a moment, and a tooltip will
appear with a description of that button.
Buttons that include both an icon and an arrow display a menu if you click on the arrow. Here is an
overview of the toolbar buttons and their functions:
Open opens a Stata dataset. Click on the button to open a dataset with the Open dialog.
Print displays a list of windows. Select a window name to print its contents.
Log begins a new log or closes, suspends, or resumes the current log. See [GSW] 16 Sav-
ing and printing results by using logs for an explanation of log files.
Viewer opens the Viewer or brings a Viewer to the front of all other windows. Click on
the button to open a new Viewer. Click on the arrow to select a Viewer to bring to the
front. See [GSW] 3 Using the Viewer for more information.
Graph brings a Graph window to the front of all other windows. Click on the button
to bring the Graph window to the front. Click on the arrow to select a Graph window
to bring to the front. See The Graph button in [GSW] 14 Graphing data for more
information.
Do-file Editor opens the Do-file Editor or brings a Do-file Editor to the front of all
other windows. Click on the button to open a new Do-file Editor. Click on the arrow to
select a Do-file Editor to bring to the front. See [GSW] 13 Using the Do-file Editor—
automating Stata for more information.
Data Editor (Edit) opens the Data Editor or brings the Data Editor to the front of the
other Stata windows. See [GSW] 6 Using the Data Editor for more information.
Data Editor (Browse) opens the Data Editor in browse mode. See Browse mode in
[GSW] 6 Using the Data Editor for more information.
Variables Manager opens the Variables Manager. See [GSW] 7 Using the Variables
Manager for more information.
Show more results tells Stata to continue when it has paused in the middle of long
output. Click on the arrow to choose whether to run the command to completion. See
[GSW] B.8 More for more information.
Break stops the current task in Stata. See [GSW] 10 Listing data and basic command
syntax for more information.
[ GSW ] 2 The Stata user interface 27
See [U] 10 Keyboard use for more information about keyboard shortcuts for the Command window.
The command history allows you to recall a previously submitted command, edit it if you wish, and
then resubmit it. Commands submitted by Stata’s dialogs are also included in the command history, so
you can recall and submit a command without having to open the dialog again.
The Variables window supports filtering and changing the display order of the variables. Text entered
in the Filter variables here field will filter the variables appearing in the Variables window. The filter is
applied to all visible columns and shows all variables that match the criteria in at least one column. By
default, the filter will ignore case and show any variables for which at least one column contains any of
the words in the filter. Clicking on the wrench on the left will allow you to change this behavior as well
as add or remove additional columns containing information about the variables.
You can change the display order of the variables in the Variables window by clicking on any column
header. The first click sorts in ascending order, the second click sorts in descending order, and the third
click puts the variables back in dataset order. Thus clicking on the Name column header will make the
Variables window display the variables in alphabetical order. Sorting in the Variables window is live,
so if you change a property of a variable when the Variables window is sorted by that property, it will
automatically move the variable to its proper location. Reordering the display order of the variables in
the Variables window does not affect the order of the variables in the dataset itself.
Right-clicking on a variable in the Variables window displays a menu from which you can select
• Keep only selected variables to keep just the selected variables in the dataset in memory. You
will be asked for confirmation. This affects only the dataset in memory, not the dataset as saved
on your disk. See [GSW] 12 Deleting variables and observations for more information.
• Drop selected variables to drop, or eliminate, the selected variables from the dataset in memory.
You will be asked for confirmation. Just as above, this affects only the dataset in memory, not
the dataset as saved on your disk. See [GSW] 12 Deleting variables and observations for more
information.
• Copy varlist to copy the selected variable names to the clipboard.
• Select all to select all variables in the dataset that satisfy the filter conditions. If no filter has been
specified, all variables will be selected.
• Send varlist to Command window to send all selected variables to the Command window.
• Font... to bring up a Font dialog, allowing you to change the font used to display the Variables
window contents.
Items from the contextual menu issue standard Stata commands, so working by right-clicking is just like
working directly in the Command window.
If you would like to hide the Variables window, click on its close button.
To reveal a hidden Variables window, select Window > Variables.
Clicking the arrow buttons next to the lock icon will select the previous or next variable shown in the
Variables window, and that selection will be reflected in the Properties window. If you would like to hide
the Properties window, click on its close box. If you would like to reveal a hidden Properties window,
select Window > Properties.
You should also investigate the Variables Manager, explained in [GSW] 7 Using the Variables Man-
ager, because it extends these capabilities and provides a good interface for managing variables.
This dialog provides access to all the functionality of Stata’s poisson command. Because the de-
pendent and independent variables must be numeric, you will find that the combo box will display only
numeric variables for choosing. The poisson command has many options that can be accessed by click-
ing on the multiple tabs across the top of the dialog. The first time you use the dialog for a command, it
is a good idea to look at the contents of each tab so that you will know all the dialog’s capabilities.
[ GSW ] 2 The Stata user interface 31
The dialogs for many commands have the by/if/in and Weights tabs. These provide access to Stata’s
commands and qualifiers for controlling the estimation sample and dealing with weighted data. See
[U] 11 Language syntax for more information on these features of Stata’s language.
The dialogs for most estimation commands have the Maximization tab for setting the maximization
options (see [R] Maximize). For example, you can specify the maximum number of iterations for the
optimizer.
Most dialogs in Stata provide the same six buttons you see at the bottom of the poisson dialog above.
OK issues a Stata command based on how you have filled out the fields in the
dialog and then closes the dialog.
Cancel closes the dialog without doing anything—just as clicking on the dialog’s
close button does.
Submit issues a command just like OK but leaves the dialog on the screen so that
you can make changes and issue another command. This feature is handy when,
for example, you are learning a new command or putting together a complicated
graph.
Help provides access to Stata’s help system. Clicking on this button will typically
take you to the help file for the Stata command associated with the dialog. Click-
ing on it here would take you to the poisson help file. The help file will have
tabs above groups of options to show which dialog tab contains which options.
Reset resets the dialog to its default state. Each time you open a dialog, it will
remember how you last filled it out. If you wish to reset its fields to their default
values at any time, simply click on this button.
Copy command to Clipboard behaves much like the Submit button, but rather
than issuing a command, it copies the command to the Clipboard. The command
can then be pasted elsewhere (such as in the Do-file Editor).
The command issued by a dialog is submitted just as if you had typed it by hand. You can see the
command in the Results window and in the History window after it executes. Looking carefully at the
full command will help you learn Stata’s command syntax.
In addition to being able to access the dialogs for Stata commands through Stata’s menus, you can
also invoke them by using two other methods. You may know the name of a Stata command for which
you want to see a dialog, but you might not remember how to navigate to that command in the menu
system. Simply type db commandname to launch the dialog for commandname:
. db poisson
You will also find access to the dialog for a command in that command’s help file; see [GSW] 4 Getting
help for more details.
As you read this manual, we will present examples of Stata commands. You may type those examples
as presented, but you should also experiment with submitting those commands by using their dialogs.
Use the db command described above to quickly launch the dialog for any command that you see in this
manual.
[ GSW ] 2 The Stata user interface 32
Window types
The Stata for Windows interface has two types of windows: docking and nondocking. The History,
Command, Variables, and Properties windows are docking windows. All other windows are nondocking.
Docking windows have special characteristics that allow them to be used with other docking win-
dows: they can be linked (sharing a window with a splitter dividing the windows) or tabbed (sharing a
window with a tab for each docking window). They can be set to automatically hide when not needed.
Nondocking windows have none of these special characteristics.
Here is a brief comparison of the two types of windows:
Docking windows
• can be linked to other docking windows so that they share a window with a splitter dividing them;
• can share one window with other docking windows with a tab for each; and
• can be made to hide automatically when not in use.
Nondocking windows
• are separate from the main Stata window (except for the Results window);
• cannot be linked to other windows;
• cannot be tabbed to another window; and
• cannot be made to hide automatically when not in use.
[ GSW ] 2 The Stata user interface 33
When Stata is first launched, the History, Results, Command, Variables, and Properties windows appear
within the main Stata window. The History, Command, Variables, and Properties windows are docking
windows and are initially both docked and linked to one another. The Results window is a nondocking
window.
Docking windows
Docking windows may be moved within the main Stata window by dragging on their title bars. When
you drag a docking window over the main Stata window or another docking window, docking guides
appear (see figure 1). Dragging the title bar over a docking guide gives you a preview of how and where
the window will appear if you release the mouse button.
Dropping the box on one of the outer docking guides links the docking window to the window under
the guide. For example, figure 1 shows how you would drag the History window over the Variables
window, at which point the docking guides appear. When you move the mouse over the bottom docking
guide and release the mouse button, the History window becomes linked to the Variables window and is
displayed in the lower part of the window.
When docking windows are linked, a divider (splitter) is inserted between them. The splitter allows
the linked windows to be resized; increasing the size of one window decreases the size of the other.
Nondocking windows
Nondocking windows include the Graph window, Data Editor, Do-file Editor, Viewers, and dialogs.
These windows are always independent of the main Stata window. The Results window, because of its
status as the primary window of Stata, must remain inside the main Stata window.
3 Using the Viewer
Viewer buttons
The toolbar of the Viewer has multiple buttons, a command box, and a search box.
Find text in page opens the find bar at the bottom of the
Viewer (see below).
35
[ GSW ] 3 Using the Viewer 36
The Find bar is used to find text within the current Viewer. To reveal the Find bar at the bottom of the
window, click on the Find text in page button:
The Find bar has its own buttons, fields, and checkboxes.
Find is the field for entering the search text you would like to
find. You can change the search options by using the check-
boxes.
Previous jumps to the previous instance of the search text; it
automatically wraps past the start of the Viewer document if
there are no previous instances of the search text.
Next jumps to the next instance of the search text; it automat-
ically wraps past the end of the Viewer document if there are
no further instances of the search text.
Highlight all highlights other instances of the search text (in
yellow, by default) when this box is checked. If unchecked,
only the current instance of the search text is highlighted (in
black, by default). By default, this box is checked.
Match case, when checked, considers uppercase and lower-
case letters to be different. When this box is checked, search-
ing for This would not find this. If unchecked, uppercase
and lowercase letters are considered the same, so searching
for This would find this. By default, this box is unchecked.
[ GSW ] 3 Using the Viewer 37
Viewer’s function
The Viewer is similar to a web browser. It has links (shown in blue text by default) that you can click
on to see related help topics and to install and manage third-party software. When you move the mouse
pointer over a link, the status bar at the bottom of the Viewer shows the action associated with that link.
If the action of a link is help logistic, clicking on that link will show the help file for the logistic
command in the Viewer. Middle-clicking on a link in a Viewer window (Ctrl+clicking if you do not
have a three-button mouse) will open the link in a new tab in the Viewer window. Shift+clicking will
open the link in a new Viewer window.
You can open a new Viewer by selecting Window > Viewer > New Viewer or by clicking on the
Viewer button on the toolbar of the main window. Entering a help command from the Command window
will also open a new Viewer.
To bring a Viewer to the front of all other Viewers, select Window > Viewer and choose a Viewer
from the list there. Selecting Close all Viewers closes all open Viewer window and tabs.
You may either type in the name of the file that you wish to view and click on OK, or you may click
on the Browse... button to open a standard file dialog that allows you to navigate to the file.
If you currently have a log file open, you may view the log file in the Viewer. This method has one
advantage over scrolling back in the Results window: what you view stays fixed even as output is added
to the Results window. If you wish to view a current log file, select File > Log > View..., and the usual
dialog will appear but with the path and filename of the current log already in the field. Simply click on
OK, and the log will appear in the Viewer. See [GSW] 16 Saving and printing results by using logs for
more details.
Printing
To print the contents of the Viewer, right-click on the window and select Print.... You may also select
File > Print > Viewer name or click on the Print toolbar button, , to print.
System help
Stata’s help system provides a wealth of information to help you learn and use Stata. To find out
which Stata command will perform the statistical or data management task you would like to do, you
should generally follow these steps:
1. Select Help > Search..., choose Search all, and enter the topic or keywords. This search will open
a new Viewer window containing information about Stata commands, references to articles in the
Stata Journal, links to Frequently Asked Questions (FAQs) on Stata’s website, links to videos on
Stata’s YouTube channel, links to selected external websites, and links to community-contributed
features.
2. Read through the results. If you find a useful command, click on the link to the appropriate com-
mand name to open its help file.
3. Read the help file for the command you chose.
4. If you want more in-depth help, click on the link from the name of the command to the PDF docu-
mentation, read it, then come back to Stata.
5. If the first help file you went to is not what you wanted, either click on the Also see menu and
choose a link to related help files or click on the Back button to go back to the previous document
and go from there to other help files.
6. With the help file open, click on the Command window and enter the command, or click on the
Dialog button and choose a link to open a dialog for the command.
7. If, at any time, you want to begin again with a new search, enter the new search terms in the search
box of the Viewer window.
8. If you select Search documentation and FAQs, Stata searches its keyword database for official
Stata commands, Stata Journal articles and software, FAQs, and videos. If you select Search net
resources, Stata searches for community-contributed commands, whether they are from the Stata
Journal or elsewhere; see [GSW] 19 Updating and extending Stata—Internet functionality for
more information.
Let’s illustrate the help system with an example. You will get the most benefit from the example if
you work along at your computer.
Suppose that we have been given a dataset about antique cars and that we need to know what it
contains. Though we still have a vague notion of having seen something like this while working through
the example session in [GSW] 1 Introducing Stata—sample session, we do not remember the proper
command.
Start by typing sysuse auto, clear in the Command window to bring the dataset into memory. (See
[GSW] 5 Opening and saving Stata datasets for information on the clear option.)
Follow the above approach:
1. Select Help > Search....
2. Check that the Search all radio button is selected.
3. Type dataset contents into the search box and click on OK or press Enter. Before we press
Enter, the window should look like
40
[ GSW ] 4 Getting help 41
4. Stata will now search for “dataset contents” among the Stata commands, the reference manuals,
the Stata Journal, the FAQs on Stata’s website, and community-contributed features. Here is the
result:
[ GSW ] 4 Getting help 42
5. Upon seeing the results of the search, we see two commands that look promising: codebook and
describe. Because we are interested in the contents of the dataset, we decide to check out the
codebook command. The [D] means that we could look up the codebook command in the Data
Management Reference Manual. The codebook link in (help codebook) means that there is a
system help file for the codebook command. This is what we are interested in right now.
6. Click on the codebook link. Links can take you to a variety of resources, such as help for Stata
commands, dialogs, and even webpages. Here the link goes to the help file for the codebook
command.
7. What is displayed is typical for help for a Stata command. Help files for Stata commands contain,
from top to bottom, these features:
a. The quick access toolbar with three buttons:
i. The Dialog button shows links to any dialogs associated with the command.
ii. The Also see button shows links to related PDF documentation and help files.
iii. The Jump to button shows links to other sections within the current help file.
b. The second line of a help file shows a View complete PDF manual entry link. Clicking on
the link will open the complete documentation for the command—in this case, codebook—
in your PDF viewer.
c. The command’s syntax, that is, rules for constructing a command that Stata will correctly
interpret. The square brackets here indicate that all the arguments to codebook are optional
but that if we wanted to specify them, we could use a varlist, an if qualifier, or an in
qualifier, along with some options. (Options vary greatly from command to command.)
The options are listed directly under the command and are explained in some detail later in
the help file. You will learn more about command syntax in [GSW] 10 Listing data and
basic command syntax.
[ GSW ] 4 Getting help 43
d. A description of the command. Because “codebook” is the name for big binders containing
a hard copy describing each of the elements of a dataset, the description for the codebook
command is justifiably terse.
e. The options that can be used with this command. These are explained in much greater detail
than in the listing of the possible options after the syntax. Here, for example, we can see
that the mv option can look to see if there is a pattern in the missing values—something
important for data cleaning and imputation.
f. Examples of command usage. The codebook examples are real examples that step through
using the command on a dataset either shipped with Stata or loadable within Stata from the
Internet.
g. The information the command stores in the returned results. These results are used primarily
by programmers.
For now, either click on Jump to and choose Examples from the drop-down menu or scroll down to
the examples. It is worth going through the examples as given in the help file. Here is a screenshot
of the top of the examples:
[ GSW ] 4 Getting help 44
Searching help
Search is designed to help you find information about statistics, graphics, data management, and
programming features in Stata, either as part of the official release or as community-contributed features.
When entering topics for the search, use appropriate terms from statistics, etc. For example, you could
enter Mann-Whitney. Multiple topic words are allowed, for example, regression residuals.
When you are using Search, use proper English and proper statistical terminology. If you already
know the name of the Stata command and want to go directly to its help file, select Help > Stata com-
mand... and type the command name. You can also type the command name in the Search field at the
top of the Viewer and press Enter.
Help distinguishes between topics and Stata commands because some names of Stata commands are
also general topic names. For example, logistic is a Stata command. If you choose Stata command...
and type logistic, you will go right to the help file for the command. But if you choose Search...
and type logistic, you will get search results listing the many Stata commands that relate to logistic
regression.
Remember that you can search for help from within a Viewer window by typing a command in the
command box of the Viewer or by clicking the magnifying glass button to the left of the search box,
selecting the scope of your search, typing the search criteria in the search box, and pressing Enter.
Most Stata reference manuals are each arranged alphabetically. Each Getting Started with Stata has
its own index. A combined index for all other manuals can be found in the Stata Index. This combined
index is a good place to start when you are looking for information about a command.
Entries have names like collapse, egen, and summarize, which are generally themselves Stata com-
mands.
Notations such as [R] ci, [R] regress, and [R] ttest in the Search results and help files are references to
the Base Reference Manual. You may also see things like [P] PyStata integration, which is a reference
to the Programming Reference Manual, and [U] 12 Data, which is a reference to the User’s Guide. For a
complete list of manuals and their shorthand notations, see Cross-referencing the documentation, which
immediately follows the table of contents in this manual.
For advice on how to use the reference manuals, see [GSW] 18 Learning more about Stata, or see
[U] 1.2 The Stata Documentation.
Stata videos
The Stata YouTube channel is an excellent resource for learning about Stata. The brief videos demon-
strate many topics using Stata’s graphical user interface. They cover basic topics, such as data man-
agement, graphics, summary statistics, and hypothesis testing, and advanced topics, such as multilevel
models and structural equation models.
There are also several playlists that provide a series of videos about a topic in sequence. For example,
the “Power and sample size calculations” playlist includes videos about how to calculate power, sample
size, and effect size for two independent proportions and for paired samples. The “Survival analysis”
playlist takes you through the process of setting your data up for survival analysis, conducting basic
descriptive analysis of survival data, graphing survival data, and calculating survivor functions and life
tables. The “Time series” playlist takes you through the process of setting your data up for time-series
analysis, creating time-series graphs, using time-series operators in estimation, and fitting ARMA and
ARIMA models. There is even a “Back-to-school video” playlist for students who are using Stata for the
first time or want a refresher after summer break.
See https://www.stata.com/links/video-tutorials/ for an up-to-date list of videos organized by topic.
The playlists can be accessed directly at https://www.youtube.com/user/statacorp/.
The Stata Journal website allows all articles older than three years to be downloaded for free.
See Downloading community-contributed commands in [GSW] 19 Updating and extending Stata—
Internet functionality for more details on how to install community-contributed software. Also see
[R] ssc for information on a convenient interface to resources available from the Statistical Software
Components (SSC) Archive.
We recommend that all users subscribe to the Stata Journal. See [U] 3.4 The Stata Journal for more
information.
Links to other sites where you can freely download programs and datasets for Stata can be found on
the Stata website; see https://www.stata.com/links/.
5 Opening and saving Stata datasets
47
[ GSW ] 5 Opening and saving Stata datasets 48
How to load a set of frames from disk and save them to disk
A set of frames, or frameset, can be saved in a single .dtas file using frames save. A frameset can
be opened with frames describe.
6 Using the Data Editor
Browse mode: Changes the Data Editor to browse mode for safely looking at data.
Open: Opens a Stata dataset. Stata will warn you if your current dataset has unsaved changes.
Paste: Pastes the contents of the Clipboard. You may paste only if one cell is selected—this
cell will become the upper-left corner of the pasted contents. Warning: This action will paste
over existing data.
49
[ GSW ] 6 Using the Data Editor 50
Find: Opens the Find bar for searching in the Data Editor.
Filter observations: Filters the observations visible in the Data Editor. This button is useful
for looking at a subset of the current dataset.
You can move about in the Data Editor by using the typical methods:
• To move to the right, use the Tab key or the right arrow key.
• To move to the left, use Shift+Tab or the left arrow key.
• To move down, use Enter or the down arrow key.
• To move up, use Shift+Enter or the up arrow key.
You can also click within a cell to select it.
Right-clicking within the Data Editor brings up a contextual menu that allows you to manipulate the
data and what you are viewing. Right-clicking on the Data Editor window displays a menu from which
you can do many common tasks:
• Copy to copy data to the Clipboard.
• Paste to paste data from the Clipboard.
• Paste special... to paste data from the Clipboard with finer control of delimiters, giving a preview
of what will be pasted.
• Select all to select all the data displayed in the Data Editor. This could be different from the data
in the dataset if the data are filtered or some variables are hidden.
• Data to open a submenu containing
• Insert variable... to bring up a dialog for creating a new variable at the current cursor
position.
• Add variable... to bring up a dialog for creating a new variable at the beginning or end of
a dataset.
• Replace contents of variable... to bring up a dialog for replacing the values of the selected
variable.
• Insert observations... to bring up a dialog for inserting new empty observations at the
current cursor position.
• Add observations... to bring up a dialog for adding new empty observations to the end of
the dataset.
• Sort data... to sort the dataset by the selected variable.
• Value labels to access a submenu for managing value labels.
• Manage value labels... to bring up value labels manager.
• Keep only selected data to keep only the selected data in the dataset. All remaining data
will be dropped (removed) from the dataset. As always, this affects only the data in memory.
It will not affect any data on disk.
• Drop selected data to drop the selected data. This is only possible if the selection consists
of either entire variables (columns) or observations (rows).
• Convert variables from string to numeric... for converting string variables to numeric
variables, which is useful when the string variables contain characters for formatting num-
bers instead of just numbers.
• Convert variables from numeric to string... for converting numeric variables to strings.
• Encode string variable to labeled numeric... for encoding a string-valued categorical vari-
able to a numeric variable while still displaying the categories in tables and graphs.
[ GSW ] 6 Using the Data Editor 51
• Decode labeled numeric variable to string... for turning an encoded variable back into a
string variable.
• Pin selected row or column to pin rows or columns. If one or more columns and variables are
selected, you will see Pin selected variables; if one or more rows and observations are selected,
you will see Pin selected observations.
• Reset selected column widths to reset the selected columns to their default widths.
• Hide selected variables to hide the selected variables.
• Show only selected variables to hide all but the selected variables.
• Show entire dataset to turn off all filters and unhide all variables.
• Preferences... to set the preferences for the Data Editor.
• Font... to change the font of the Data Editor.
Data entry
Entering data into the Data Editor is similar to entering data into a spreadsheet. One major difference is
that the Data Editor has the concept of observations, which makes the data entry smart. We will illustrate
this with an example. It will be useful for you to follow the example at your computer. To work along,
you will need to start with an empty dataset, so save your dataset if necessary, and then type clear in
the Command window.
Note: As a check to see if your data have changed, type describe, short (or d,s for short). Stata
will tell you if your data have changed.
Suppose that we have the following data, and we want to enter them into Stata:
Make Price MPG Weight Gear ratio
VW Rabbit 4697 25 1930 3.78
Olds 98 8814 21 4060 2.41
Chev. Monza 3667 2750 2.73
AMC Concord 4099 22 2930 3.58
Datsun 510 5079 24 2280 3.54
5189 20 3280 2.93
Datsun 810 8129 21 2750 3.55
We do not know MPG for the third car or the make of the sixth.
Start by opening the Data Editor in edit mode. You can do this either by clicking on the Data Editor
(Edit) button, , or by typing edit in the Command window. You should be greeted by a Data Editor
with no data displayed. (If you see data, type clear in the Command window.) Stata shows the active
cell by highlighting it and displaying varname[obsnum] next to the input box in the Cursor Location
box. We will see below that we can navigate within a dataset by using this cell reference. The Data
Editor starts, by default, in the first row of the first column. Because there are no data, there are no
variable names, and so Stata shows var1[1] as the active cell.
We can enter these data either by working across the rows (observation by observation) or by working
down the columns (variable by variable). To enter the data observation by observation, press Tab after
entering each value until you have reached the end of the first row. In our case, we would type VW Rabbit,
press Tab, type 4697, press Tab, and continue entering data to complete the first observation.
[ GSW ] 6 Using the Data Editor 52
After you are finished with the first observation, select the second cell in the first column, either by
clicking within it or by navigating to it. At this point, your screen should look like this:
We can now enter the data for the second observation in the same fashion as the first—with one nice
difference: after we enter the last value in the row, pressing the Tab key will bring us to the first cell in
the third row. This is possible because the number of variables is known after the first observation has
been entered, so Stata knows when it has all the data for an observation.
We can enter the rest of the data by pressing the Tab key between entries, simply skipping over missing
values by tabbing through them.
If we had wanted to enter the data variable by variable, we could have done that by pressing Enter
between each make of car until all seven observations were entered, skipping past the missing entry by
pressing Enter twice. Once the first variable was entered, we would select the first cell in the second
column and enter the price data. We would continue this until we were finished.
Click on the first cell in the eighth row, right-click while you are still in that cell, and choose Paste
from the resulting menu. You can see that the observation was successfully duplicated.
tables in some delimited form, as do spreadsheet applications, many database applications, and
some word processors. Using Edit > Paste special... gives some added flexibility to the formats
you can paste into the Data Editor. If a simple paste does not give you what you expected, you
should try Edit > Paste special.... For more information on file-based methods for importing data
into Stata, see [GSW] 8 Importing data.
• If you are copying and pasting data with value labels, you have a choice. You can copy variables
with value labels as text, using the value labels as the actual values, or you can copy said variables
as their underlying encoded numbers. Copying with the value labels is the default. If you would
like the other choice, select View > Show all value labels.
Changing data
As its name suggests, the Data Editor can be used to edit your dataset. As we have seen already, it
can be used to edit the data themselves as well as the description and display options for the variables.
Here is an example for making some changes to the automobile dataset, which illustrates both methods
for using the Data Editor and its documentation trail. We will also keep snapshots of the dataset as we
are working so that we can revert to previous versions of the dataset in case we make a mistake.
We would like to investigate the dataset, work with value labels, delete the trunk variable, and make
a new variable showing gas consumption per 100 miles. These tasks will illustrate the basics of working
in the Data Editor.
Start by typing sysuse auto into the Command window. If you worked the previous example, you
will get an error and are told that the dataset in memory has changed since it was last saved. This is good—
Stata is keeping you from inadvertently throwing away the unsaved changes to your current dataset as it
loads auto.dta. If you would like to save the dataset you have been working on, select File > Save and
save the dataset in an appropriate location. Otherwise, type clear in the Command window, and press
Enter to clear out the data, and then load that auto.dta.
Once auto.dta is loaded, start the Data Editor.
1. We remember that our grandfather had a Toronado, which looked sleek but seemed to require a lot
of fill-ups. We would like to see if this car is in the dataset. To find it, we select Edit > Find...,
type Toronado, and press Enter. We see that this make of car got 16 miles per gallon.
2. We would like to see which cars have the lowest and highest gas mileages. To do this, right-click
on the column heading of the mpg column. Select Data > Sort data... from the contextual menu.
A dialog will pop up asking how you want to sort, defaulting to sorting in ascending order. Click
on OK. (Stata worries about sort order because sort order can affect reproducibility when using
resampling techniques. This is a good thing.) You will see that the data have now been sorted
by mpg in ascending order. The lowest-mileage cars are at the top of the screen; by scrolling to
the bottom of the dataset, you can find the highest-mileage cars. You also could have sorted by
selecting Tools > Sort data... once the mpg variable was selected.
3. We would like to investigate repair records and hence sort by the rep78 variable. (Do this now.)
We see that the Starfire and Firebird both had poor repair records, but we would like to see the
cars with good repair records. We could scroll to the bottom of the dataset, but it will be faster to
use the Cursor Location box: type rep78 74 and press Enter to make rep78[74] the active cell.
We notice that the last five entries for rep78 appear as dots. The dots mean that these values are
missing. A few items of note:
[ GSW ] 6 Using the Data Editor 57
• As we can see from the result of the sort, Stata views missing values as being larger than all
numeric nonmissing values. In technical terms, this means that rep78 >= . is equivalent to
missing(rep78).
• What we do not see here is that Stata has multiple missing-value indicators: . is Stata’s
default or system missing-value indicator, and .a, .b, . . . , .z are Stata’s extended missing
values. Extended missing values are useful for indicating the reason why a value is unknown.
• The different missing values sort among themselves: . < .a < .b < · · · < .z. See
[U] 12.2.1 Missing values for full details.
4. We would like to make the repair records readable. Click on rep78 in the Variables window.
5. Click on the Value label field in the Properties window, and then click on the ellipsis (. . .) button
that appears. This opens the Manage value labels dialog. We need to define a new value label for
the repair records.
a. Click on the Create label button. You will see the Create label dialog.
b. Type a name for the label, say, repairs, in the Label name box.
c. Press the Tab key or click within the Value field.
d. Type 1 for the value, press the Tab key, and type atrocious for the label.
e. Press the Enter key to create the pairing.
f. Repeat steps d and e to make all the pairings: 2 with “bad”, 3 with “OK”, 4 with “good”,
and 5 with “stupendous”.
g. Click on the OK button to finish creating the value label.
h. Click on the disclosure control, , to show the label—you should see this:
If you have something else, you can edit the label by clicking on the Edit label button.
i. Click on the Close button to close the Manage value labels dialog.
Now that the label has been created, attach it to the rep78 variable by clicking on the down arrow
in the Value label field and selecting the repairs label. You can now see the labels displayed in
place of the values.
6. Suppose that we found the original source of the data in a time capsule, so we could replace some of
the missing values for rep78. We could type the values into cells. We can also assign the values by
right-clicking within a cell with a missing value and choosing a value from Data > Value labels >
Assign value from value label “repairs”. This strategy can be useful when a value label has many
possible values.
[ GSW ] 6 Using the Data Editor 58
7. We would now like to delete the trunk variable. We can do this by right-clicking on the trunk
variable name at the top of the column and selecting the Data > Drop selected data menu item.
Because this can lead to data loss, the Data Editor asks whether we would like to drop the selected
variable. Click on the Yes button.
8. To finish up, we would like to create a variable containing the gallons of gasoline per 100 miles
driven for each of the cars.
a. Right-click within any cell, and choose the Data > Add variable... menu item to bring up
the generate dialog.
b. Type gp100m in the Variable name field.
c. Being sure that the Specify a value or an expression radio button is selected, type 100/mpg in
its field. We could have clicked on the Create... button to open the Expression Builder dia-
log, but this formula was simple enough to type. (You might want to explore the Expression
Builder right now to see what it can do.)
d. Be sure that the Add at the end of dataset item is chosen from the Position of new variable
list.
e. Click on OK. You can scroll to the right to see the newly created variable.
Throughout this data-editing session, we have been using the Data Editor to manipulate the data.
If you look in the Results window, you will see the commands and their output. You can also see all
the commands generated by the Data Editor in the History window. If you wanted to save the editing
commands to use again later, you could do the following steps:
1. Click in the History window on the last command that came from the Data Editor.
2. Scroll up until you find the sort mpg command you ran immediately after opening the Data Editor,
and Shift-click on it.
3. Right-click on one of the highlighted commands.
4. Select Send selected to Do-file Editor.
This procedure will save all the commands you highlighted into the Do-file Editor. You could then
save them as a do-file, which you could run again later. We will talk more about the Do-file Editor in
[GSW] 13 Using the Do-file Editor—automating Stata. You can find help about do-files in [U] 16 Do-
files.
If you want to save this dataset, save it under a new name by using File > Save as... in the main Stata
window to prevent overwriting the original dataset.
We will keep using auto.dta from above; if you are starting here, you can start fresh by typing
sysuse auto in the Command window to open the dataset. (If you get a warning about data in memory
being lost, either use clear or save your data. See [GSW] 5 Opening and saving Stata datasets for
more information.) If we open the Data Editor and click on the Snapshots tab beneath the Variables
window, we see the following window. If you are starting afresh, you will see numbers rather than labels
for rep78.
To begin with, only one button is active in the Snapshots toolbar. Click on the active button—the Add
button, . It brings up a dialog asking for a label, or name, for the snapshot. Give it an inventive name,
such as Start, and press Enter. You can see that a snapshot is now listed in the Snapshots window, and
all the buttons in the toolbar are now active. The following buttons appear in the Snapshots window:
Add: Save a new snapshot with a timestamp and label.
Remove: Erase a snapshot. This action deletes the temporary snapshot file but does not
affect the data in memory.
Restore: Replace the data in memory with the data from the selected snapshot. You will
get a dialog asking you to confirm your action.
You should now try manipulating the dataset by using the tools we have seen. Once you have done
that, create another snapshot, calling it Changed. Open the Snapshots window and restore the Start
snapshot by either double-clicking it or clicking first on it and then on the Restore button to see where
you started. You can then go back to where you were working by restoring your Changed snapshot.
Snapshots continue to be available either until they are deleted or until you exit Stata. You can thus
use snapshots of one dataset while working on another. You will find your own uses for snapshots—just
take care to save the datasets you want for future use because the snapshots are temporary.
[ GSW ] 6 Using the Data Editor 60
You can see a date variable that has January 2, 2001, as its first day, though it is being displayed in
Stata’s default format for dates.
We will start with formatting:
1. Select the date variable in the Variables window to the right of the data table.
2. In the Properties window, select the Format row and click on the ellipsis button that appears.
3. The Create format dialog tells us three pieces of information about the date format:
• These are daily dates. As you can see, Stata understands other types of dates that are often
used in financial data.
• Looking at the bottom of the dialog, you can see that Stata’s default date format is %td. This
means that the variable contains time values that are to be interpreted as daily dates.
• This default format is displayed as, for example, 07apr2021.
4. There are many premade date formats in the Samples pane at the top right of the Create format
dialog. Click on April 7, 2021. You can see how the format would be specified at the bottom of
the dialog.
5. Click on OK to close the Create format dialog. You can see that the dates are now displayed
differently.
This is a very simple way to change date formats. For complete information on dates and date formats,
see [D] Datetime.
We will now change some of the dates to illustrate how this can be done simply, regardless of the
format in which the dates are displayed. If you look in the upper-right corner of the Data Editor, you
will see the Time/Date input mask field, which shows DMY. This field affects how dates are entered when
editing data.
[ GSW ] 6 Using the Data Editor 61
By default, the input mask is set to DMY. This means dates can be entered in many different fashions,
as long as the order of the date components is day, month, year. Try the following:
1. Click in the first observation of date so that the Cursor Location shows date[1].
2. Type 18jan2021 and press the Enter key. Stata understands the DMY input mask and knows enough
to enter the new date in the selected cell.
3. Enter 30042021 and press Enter. Stata still understands the input mask, even though there are no
separators.
4. Click within the Time/Date input mask field, and choose MDY from the drop-down menu.
5. Click on any observation in the date column.
6. Type March 15, 2021 and press Enter. Stata will still understand.
Working in this fashion is the fastest way to edit dates by hand. If you look in the Results window, you
will see why.
We are now finished with this dataset, so type clear and press Enter.
Suppose that we would like to edit only those observations for which rep78 is missing. We will need
to look at the make of the car so that we know which observations we are working with, but we do not
need to see any other variables. We will work as though we had a very large dataset to work with.
1. Before we get started, try experimenting with the Variables window.
a. Drag variables up and down the list. Doing so changes the order of the variables’ columns
in the Data Editor. It does not change their order in the dataset itself.
b. Uncheck some of the checkboxes in the first column to hide some of the variables.
c. Type a search criterion in the Filter variables here field. Just like in the Variables window in
the main Stata window, the default is to ignore case and find any variables or variable labels
containing any of the words in the filter. Clicking on the wrench on the left will allow you to
change this behavior as well as to add or remove additional columns containing information
about the variables. The filtering of variables in the list affects what is displayed in the
Variables window; it does not affect what variables’ data are displayed. When you are done,
delete your filter text.
2. Right-click on any variable in the Variables window, and select Select all from the contextual
menu.
3. Click on any checkbox to deselect all the variables.
4. Click on the make variable to select it, and deselect all the other variables.
5. Click on the checkbox for make.
6. Click on the checkbox for rep78.
If you look in the Command window, you can see that no commands have been issued, because hiding
the variables does not affect the dataset—it affects only what shows in the Data Editor.
We now have protected ourselves by using only those variables that we need. We should now reduce
our view to only those observations for which rep78 is missing. This is simple.
1. Click on the Filter observations button, , in the Data Editor’s toolbar.
2. Enter missing(rep78) in the Filter by expression field.
3. Click on the Apply filter button.
4. If you are curious, click on the ellipsis button. It opens up an Expression Builder dialog. This lists
the wide variety of functions available in Stata. See the Stata Functions Reference Manual.
Now we are focused on the part of the dataset in which we would like to work, and we cannot destroy or
mistakenly alter other data by stray keystrokes in the Data Editor window.
It is worth learning how to hide variables and filter observations in the Data Editor from the Command
window. This can be quite convenient if you are going to restrict your view, as we did above. To work
from the Command window, we must use the edit command together with a varlist (variable list) along
with if and in qualifiers in the Command window. By using a varlist, we restrict the variables we
look at, whereas the if and in qualifiers restrict the observations we see. ([GSW] 10 Listing data and
basic command syntax contains many examples of using a command with a variable list and if and
in.) Suppose we want to correct the missing values for rep78. The minimum amount of data we need
[ GSW ] 6 Using the Data Editor 63
to expose are make and rep78. To see this minimal amount of information and hence to minimize our
exposure to making mistakes, we enter the commands
. sysuse auto
(1978 automobile data)
. edit make rep78 if missing(rep78)
Browse mode
The purpose of using the Data Editor in browse mode is to look at data without altering them by stray
keystrokes. You can start the Data Editor in browse mode by clicking on the Data Editor (Browse) but-
ton, , or by typing browse in the Command window. When you work in browse mode, all contextual
menu items that would let you alter the data, the labels, or any of the display formats for the variables are
disabled. You may view a variable’s properties with the Properties menu item, but you may not make
any changes. You still can filter observations and hide variables to get a restricted view because these
actions do not change the dataset.
Note: Because you can still use Stata menus not related to the Data Editor and because you can still
type commands in the Commands window, it is possible to change the data even if the Data Editor is in
browse mode. In fact, this means you can watch how your commands affect the dataset. You are merely
restricted from using the Data Editor itself to change the data.
[ GSW ] 6 Using the Data Editor 64
Drag the column divider and make sure all variable labels are fully displayed. Column widths are
preserved when saving the dataset. When you have a string value that is longer than the column width,
the value displayed in the cell view will be truncated. The Data Editor has tooltip support for truncated
cell views. You can hover the mouse pointer over the cell to view the full string value in a tooltip. You
can also double-click in the cell to bring up a resizeable cell editor for long strings.
[ GSW ] 6 Using the Data Editor 65
If you first click on a column header to highlight a particular variable, such as state, right-clicking
brings up a similar menu but with the option Pin selected variables enabled. More than one variable
can be selected and pinned.
After pinning the state information, we can horizontally scroll to the right to see other variables that
previously could not fit in the window, such as marriage and divorce, with the state information pinned
on the left.
[ GSW ] 6 Using the Data Editor 67
Similarly, you can highlight one or more observations and right-click to pin a particular row or multiple
rows.
Let’s try pinning the observations from 1 through 3, and then let’s vertically scroll down to other states
and compare them with the first three.
The pinning feature can be invoked in both browse and edit modes. You can pin both rows and columns
at the same time. Right-click on any pinned rows or columns, and select Clear pinned variables or
observations to unpin them.
7 Using the Variables Manager
The Variables Manager is a tool for managing properties of variables both individually and in groups.
It can be used to create variable and value labels, rename variables, change display formats, and manage
notes. It has the ability to filter and group variables as well as to create variable lists. Users will find
these features useful for managing large datasets.
Any action you take in the Variables Manager results in a command being issued to Stata as though you
had typed it in the Command window. This means that you can keep good records and learn commands
by using the Variables Manager.
• The order of the columns can be changed by dragging the column titles. To restore the original
column headings, right-click on the column titles and select Restore column defaults.
• The variables can be grouped by values in one or more columns. This is done by dragging the
column titles into the grouping bar. The grouping can be canceled by dragging the column titles
back into the column titles row. Here is an example of auto.dta grouped by variable type:
Managing notes
Stata allows you to attach notes to both variables and the dataset as a whole. These are simple text
notes that you can use to document whatever you like—the source of the dataset, data collection quirks
associated with a variable, what you need to investigate about a variable, or anything else.
Start by selecting a variable in the Variable pane. We will work with the price variable. Click on the
Manage... button next to the Notes field, and you will see the following dialog appear:
[ GSW ] 7 Using the Variables Manager 71
It is worth experimenting with adding, deleting, and editing notes. Notes can be an invaluable memory
aid when working on projects that last a long time. Anytime you manipulate notes in the Notes Manager,
you create Stata commands.
8 Importing data
You would like to put these data into Stata. Doing so is easier than you think:
1. Clear out your current dataset by typing clear.
2. Copy the above data.
3. Open the Data Editor in edit mode.
4. Select Edit > Paste special....
5. Stata sees that the column delimiters are commas and shows how the data would look.
6. Click on the OK button.
You can see that Stata has imported the data nicely.
Later in this chapter, we would like to bring these data into Stata without copying and pasting, so we
would like to save them as a text file. Go back to the main Stata window, and click on the Do-file Editor
button, , to open a new Do-file Editor window. Paste the data in the Do-file Editor, then click on the
Save button. Navigate to your working directory, and save the file as a few cars.csv. If you do not
know what your working directory is, look in the status bar at the bottom of the main Stata window.
Be careful if you are copying data from a spreadsheet because spreadsheets can contain special for-
matting that ruins its rectangular form. Be sure that your spreadsheet does not contain blank rows, blank
columns, repeated headers, or merged cells because these can cause trouble. As long as your spreadsheet
looks like a table, you will be fine.
• import delimited, which is made for reading text files created by spreadsheet or database pro-
grams or, more generally, for reading text files with clearly defined column delimiters such as
commas, tabs, semicolons, or spaces;
• infile, which is made for reading simple data that are separated by spaces or rigidly formatted
data aligned in columns; and
• infix, which is made for data aligned in columns but possibly split across rows.
Stata has other commands that can read other types of files and can even get data from external
databases without the need for an interim file:
• The import excel command can read Microsoft Excel files directly, either as an .xls or as an
.xlsx file.
• The import sas command can read native SAS files, so data can be transferred from SAS to Stata
in this fashion.
• The import spss command can read IBM SPSS Statistics files.
• The import sasxport5 command can read version SAS V5 Transport files. The import
sasxport8 command can read version SAS V8 Transport files.
• The odbc command can be used to pull data directly from any data sources for which you have
ODBC (Open Database Connectivity) drivers.
• The jdbc command allows you to load data from a database, execute SQL statements on a database,
and insert data into a database using JDBC (Java Database Connectivity) drivers.
Stata can import more formats; see [D] import for the full list.
Each command expects the file that it is reading to be in a specific format. This chapter will explain
some of those formats and give some examples. For the full description, consult the Data Management
Reference Manual.
. clear
. import delimited "a few cars.csv"
(encoding automatically selected: ISO-8859-9)
(5 vars, 7 obs)
You can look at the data in the Data Editor, and it will look just like the earlier result from copying
and pasting. We will now list the data so that we can see them in the manual. The separator(0) option
suppresses the horizontal separator line that is drawn after every fifth observation by default.
. list, separator(0)
v1 v2 v3 v4 v5
If you want to specify better variable names, you can include the desired names in the command.
When you specify variable names, you must also use the using keyword before the filename.
. import delimited make price mpg weight gear_ratio using "a few cars.csv"
(encoding automatically selected: ISO-8859-9)
(5 vars, 7 obs)
. list, separator(0)
As a side note about displaying data, Stata listed gear ratio as gear r~o in the output from list.
gear r~o is a unique abbreviation for the variable gear ratio. Stata displays the abbreviated variable
name when variable names are longer than eight characters.
[ GSW ] 8 Importing data 75
To prevent Stata from abbreviating gear ratio, you could specify the abbreviate(10) option:
. list, separator(0) abbreviate(10)
For more information on the ~ abbreviation and on list, see [GSW] 10 Listing data and basic
command syntax.
We will use this dataset again in the next chapter, so we would like to save it. Type save afewcars,
and press Enter in the Command window to save the dataset.
For this simple example, you could have copied the contents of the file and pasted it into the Data
Editor by using Paste special... and choosing comma as the delimiter.
For text files that have no nice delimiters or for which observations could be spread out across many
lines, Stata has two more commands: infile and infix. See [D] import for more information about
how to read in such files.
The data allow us to make some guesses at the values in the dataset, but, for example, we do not
know the units in which the price or weight is measured, and the term “mpg” could be confusing for
people outside the United States. Perhaps we can learn something from the description of the dataset.
Stata has the aptly named describe command for this purpose (as we saw in [GSW] 1 Introducing
Stata—sample session).
77
[ GSW ] 9 Labeling data 78
. describe
Contains data from afewcars.dta
Observations: 7
Variables: 5 20 Nov 2024 06:27
Sorted by:
Though there is precious little information that could help us as a researcher, we can glean some
information here about how Stata thinks of the data from the first three columns of the output.
1. The Variable name is the name we use to tell Stata about a variable.
2. The Storage type (otherwise known as the data type) is the way in which Stata stores the data in a
variable. There are six different storage types, each having its own memory requirement:
a. byte for integers between −127 and 100 (using 1 byte of memory per observation)
b. int for integers between −32,767 and 32,740 (using 2 bytes of memory per observation)
c. long for integers between −2,147,483,647 and 2,147,483,620 (using 4 bytes of memory per ob-
servation)
d. float for real numbers with 8.5 digits of precision (using 4 bytes of memory per observation)
e. double for real numbers with 16.5 digits of precision (using 8 bytes of memory per observation)
f. For strings (text) between 1 and 2,045 bytes (using 1 byte of memory per observation per character
for ASCII and up to 4 bytes of memory per Unicode character):
str1 for 1-byte-long strings
str2 for 2-byte-long strings
str3 for 3-byte-long strings
...
str2045 for 2,045-byte-long strings
Stata also has a strL storage type for strings of arbitrary length up to 2,000,000,000 bytes. strLs
can also hold binary data, often referred to as BLOBs, or binary large objects, in databases. We will
not illustrate these here.
Storage types affect both the precision of computations and the size of datasets. A quick guide to
storage types is available at help data types or in [D] Data types.
3. The Display format controls how the variable is displayed; see [U] 12.5 Formats: Controlling how
data are displayed. By default, Stata sets it to something reasonable given the storage type.
We want to make this dataset into something containing all the information we need.
[ GSW ] 9 Labeling data 79
To see what a well-labeled dataset looks like, we can look at a dataset stored at the Stata Press repos-
itory. We need not load the data (and disturb what we are doing); we do not even need a copy of the
dataset on our machine. (You will learn more about Stata’s Internet capabilities in [GSW] 19 Updating
and extending Stata—Internet functionality.) All we need to do is direct describe to look at the
proper file by using the command describe using filename.
. describe using https://www.stata-press.com/data/r18/auto
Contains data 1978 automobile data
Observations: 74 13 Apr 2022 17:45
Variables: 12
This output is much more informative. There are three locations where labels are attached that help
explain what the dataset contains:
1. In the first line, 1978 automobile data is the data label. It gives information about the contents of
the dataset. Data can be labeled by selecting Data > Data utilities > Label utilities > Label dataset,
by using the label data command, or by editing the Label field in the Data portion of the Properties
window. When doing this in the main window, be sure that the Properties window is unlocked.
2. There is a variable label attached to each variable. Variable labels are how we would refer to the
variable in normal, everyday conversation. Here they also contain information about the units of the
variables. Variables can be labeled by selecting the variable in the Variables window and editing the
Label field in the Properties window. You can also change a variable label by using the Variables
Manager or by using the label variable command.
3. The foreign variable has an attached value label. Value labels allow numeric variables such as
foreign to have words associated with numeric codes. The describe output tells you that the
numeric variable foreign has value label origin associated with it. Although not revealed by
describe, the variable foreign takes on the values 0 and 1, and the value label origin associates
0 with Domestic and 1 with Foreign. If you browse the data (see [GSW] 6 Using the Data Edi-
tor), foreign appears to contain the values “Domestic” and “Foreign”. The values in a variable are
labeled in two stages. The value label must first be defined. This can be done in the Data Editor, or
in the Variables Manager, or by selecting Data > Data utilities > Label utilities > Manage value
labels or by typing the label define command. After the labels have been defined, they must be
attached to the proper variables, either by selecting Data > Data utilities > Label utilities > Assign
[ GSW ] 9 Labeling data 80
value label to variables or by using the label values command. Note: It is not necessary for the
value label to have a name different from that of the variable. You could just as easily have used a
value label named foreign.
Sorted by:
. label data "A few 1978 cars"
. label variable make "Make and model"
. label variable price "Price (USD)"
. label variable mpg "Mileage (miles per gallon)"
. label variable weight "Vehicle weight (lbs.)"
. label variable gear_ratio "Gear ratio"
. describe
Contains data from afewcars.dta
Observations: 7 A few 1978 cars
Variables: 5 20 Nov 2024 06:28
Sorted by:
Note: Dataset has changed since last saved.
. save afewcars2
file afewcars2.dta saved
[ GSW ] 9 Labeling data 81
You can create this new variable in the Data Editor if you would like to work along. (See [GSW] 6 Us-
ing the Data Editor for help with the Data Editor.) Though the definitions of the categories “0” and
“1” are clear in this context, it still would be worthwhile to give the values explicit labels because it will
make output clear to people who are not so familiar with antique automobiles. This is done with a value
label.
We saw an example of creating and attaching a value label by using the point-and-click interface
available in the Data Editor in Changing data in [GSW] 6 Using the Data Editor. Here we will do it
directly from the Command window.
. label define origin 0 "Domestic" 1 "Foreign"
. label values foreign origin
. describe
Contains data from afewcars2.dta
Observations: 7 A few 1978 cars
Variables: 6 20 Nov 2024 06:28
Sorted by:
Note: Dataset has changed since last saved.
. save afewcarslab
file afewcarslab.dta saved
[ GSW ] 9 Labeling data 82
From this example, we can see that a value label is defined via
label define labelname # ”contents” # ”contents” . . .
It can then be attached to a variable via
label values variablename labelname
Once again, we need to save the dataset to be sure that we do not mistakenly lose the labels later. We
saved this under a new filename because we have cleaned it up, and we would like to use it in the next
chapter.
If you had wanted to define the value labels by using a point-and-click interface, you could do this with
the Properties window in either the Main window or the Data Editor or by using the Variables Manager.
See [GSW] 7 Using the Variables Manager for more information.
There is more to value labels than what was covered here; see [U] 12.6.3 Value labels for a complete
treatment.
You may also add notes to your data and your variables. This feature was previously discussed in Re-
naming and formatting variables in [GSW] 6 Using the Data Editor and Managing notes in [GSW] 7 Using
the Variables Manager. You can learn more about notes by typing help notes, or you can get the full
story in [D] notes.
10 Listing data and basic command syntax
Command syntax
This chapter gives a basic lesson on Stata’s command syntax while showing how to control the ap-
pearance of a data list.
As we have seen throughout this manual, you have a choice between using menus and dialogs and
using the Command window. Although many find the menus more natural and the Command window
baffling at first, some practice makes working with the Command window often much faster than using
menus and dialogs. The Command window can become a faster way of working because of the clean
and regular syntax of Stata commands. We will cover enough to get you started; help language has
more information and examples, and [U] 11 Language syntax has all the details.
The syntax for the list command can be seen by typing help list:
list [ varlist ] [ if ] [ in ] [ , options ]
83
[ GSW ] 10 Listing data and basic command syntax 84
. list
1. VW Rabbit 25 4697
2. Olds 98 21 8814
3. Chev. Monza . 3667
4. 22 4099
5. Datsun 510 24 5079
. list m*
make mpg
1. VW Rabbit 25
2. Olds 98 21
3. Chev. Monza .
4. 22
5. Datsun 510 24
6. Buick Regal 20
7. Datsun 810 .
. li price-weight
1. 4697 25 1930
2. 8814 21 4060
3. 3667 . 2750
4. 4099 22 2930
5. 5079 24 2280
6. 5189 20 3280
7. 8129 . 2750
[ GSW ] 10 Listing data and basic command syntax 86
. list ma?e
make
1. VW Rabbit
2. Olds 98
3. Chev. Monza
4.
5. Datsun 510
6. Buick Regal
7. Datsun 810
. l gear_r~o
gear_r~o
1. 3.78
2. 2.41
3. 2.73
4. 3.58
5. 3.54
6. 2.93
7. 3.55
list with if
The if qualifier uses a logical expression to determine which observations to use. If the expression
is true, the observation is used in the command; otherwise, it is skipped. The operators whose results are
either true or false are
In the logical expressions, & is evaluated before | (similar to multiplication before addition in arith-
metic). You can use this in your expressions, but it is often better to use parentheses to ensure that the
expressions are evaluated in the proper order. See [U] 13.2 Operators for complete details.
[ GSW ] 10 Listing data and basic command syntax 87
. list
. list make mpg price gear if (mpg > 22) | (price > 8000 & gear < 3.5)
make mpg
2. Olds 98 21
4. 22
In the listings above, we see more examples of Stata treating missing numerical values as large values,
as well as the care that should be taken when the if qualifier is applied to a variable with missing values.
See [GSW] 6 Using the Data Editor.
[ GSW ] 10 Listing data and basic command syntax 88
. list if mpg=21
=exp not allowed
r(101);
The error arises because “equal” is expressed by ==, not by =. Corrected, it becomes
. list if mpg==21
Joint tests are specified with &, not with the word and or multiple ifs. The if qualifier should be if
mpg==21 & weight>4000, not if mpg==21 if weight>4000. Here is its correction:
. list if mpg==21 & weight > 4000
Strings must be in double quotes, as in make==”Datsun 510”. Without the quotes, Stata thinks that
Datsun is a variable that it cannot find. Here is the correction:
. list if make=="Datsun 510"
Value labels look like strings, but the underlying variable is numeric. Variable foreign takes on
values 0 and 1 but has the value label that attaches 0 to “Domestic” and 1 to “Foreign” (see [GSW] 9 La-
beling data). To see the underlying numeric values of variables with labeled values, use the label list
command (see [D] label), or investigate the variable with codebook varname. We can correct the error
here by looking for observations where foreign==0.
[ GSW ] 10 Listing data and basic command syntax 90
There is a second construction that also allows the use of the value label directly.
. list if foreign==0
. list if foreign=="Domestic":origin
[ GSW ] 10 Listing data and basic command syntax 91
list with in
The in qualifier uses a numlist to give a range of observations that should be listed. numlists have the
form of one number or first /last. Positive numbers count from the beginning of the dataset. Negative
numbers count from the end of the dataset. Here are some examples:
. list
. list in 1
. list in -1
. list in 2/4
. list in -3/-2
[ GSW ] 10 Listing data and basic command syntax 92
1. 2930 3.58
2. Buick Regal 3280 2.93
3. Chev. Monza 2750 2.73
4. Olds 98 4060 2.41
5. Datsun 510 2280 3.54
. list, divider
[ GSW ] 10 Listing data and basic command syntax 93
The separator() option draws a horizontal line at specified intervals. When not specified, it defaults
to a value of 5.
. list, separator(3)
Break
If you want to interrupt a Stata command, click on the Break button, .
It is always safe to click on the Break button. After you click on Break, the state of the system is the
same as if you had never issued the original command.
11 Creating new variables
Relational
Arithmetic Logical (numeric and string)
+ addition ! not > greater than
- subtraction | or < less than
* multiplication & and >= > or equal
/ division <= < or equal
^ power == equal
!= not equal
+ string concatenation
Stata has many mathematical, statistical, string, date, time-series, and programming functions. See
help functions for the basics, and see the Stata Functions Reference Manual for a complete list and
full details of all the built-in functions.
You can use menus and dialogs to create new variables and modify existing variables by selecting
menu items from the Data > Create or change data menu. This feature can be handy for finding
functions quickly. However, we will use the Command window for the examples in this chapter because
we would like to illustrate simple usage and some pitfalls.
Stata has some utility commands for creating new variables:
• The egen command is useful for working across groups of variables or within groups of observa-
tions. See [D] egen for more information.
• The encode command turns categorical string variables into encoded numeric variables, while its
counterpart decode reverses this operation. See [D] encode for more information.
• The destring command turns string variables that should be numeric, such as numbers with
currency symbols, into numbers. To go from numbers to strings, the tostring command is useful.
See [D] destring for more information.
We will focus our efforts on generate and replace.
94
[ GSW ] 11 Creating new variables 95
generate
There are some details you should know about the generate command:
• The basic form of the generate command is generate newvar = exp, where newvar is a new
variable name and exp is any valid expression. You will get an error message if you try to generate
a variable that already exists.
• An algebraic calculation using a missing value yields a missing value, as does division by zero,
the square root of a negative number, or any other computation which is impossible.
• If missing values are generated, the number of missing values in newvar is always reported. If
Stata says nothing about missing values, then no missing values were generated.
• You can use generate to set the storage type of the new variable as it is generated. You might
want to create an indicator (0/1) variable as a byte, for example, because it saves 3 bytes per
observation over using the default storage type of float.
Below are some examples of creating new variables from the afewcarslab dataset, which we cre-
ated in Labeling values of variables in [GSW] 9 Labeling data. (To work along, start by opening the
automobile dataset with sysuse auto. We are using a smaller dataset to make shorter listings.) The
last example shows a way to generate an indicator variable for cars weighing more than 3,000 pounds.
Logical expressions in Stata result in 1 for “true” and 0 for “false”. The if qualifier is used to ensure
that the computations are done only for observations where weight is not missing.
[ GSW ] 11 Creating new variables 96
. use afewcarslab
(A few 1978 cars)
. list make mpg weight
1. VW Rabbit 25 1930
2. Olds 98 21 4060
3. Chev. Monza . 2750
4. 22 2930
5. Datsun 510 24 2280
[ GSW ] 11 Creating new variables 97
replace
Whereas generate is used to create new variables, replace is the command used for existing vari-
ables. Stata uses two different commands to prevent you from accidentally modifying your data. The
replace command cannot be abbreviated. Stata generally requires you to spell out completely any com-
mand that can alter your existing data.
. list make weight
make weight
1. VW Rabbit 1930
2. Olds 98 4060
3. Chev. Monza 2750
4. 2930
5. Datsun 510 2280
make weight
1. VW Rabbit 1.93
2. Olds 98 4.06
3. Chev. Monza 2.75
4. 2.93
5. Datsun 510 2.28
Suppose that you want to create a new variable, predprice, which will be the predicted price of the
cars in the following year. You estimate that domestic cars will increase in price by 5% and foreign cars,
by 10%.
[ GSW ] 11 Creating new variables 98
One way to create the variable would be to first use generate to compute the predicted domestic car
prices. Then use replace to change the missing values for the foreign cars to their proper values.
. gen predprice = 1.05*price if foreign==0
(3 missing values generated)
. replace predprice = 1.10*price if foreign==1
(3 real changes made)
. list make foreign price predprice, nolabel
Of course, because foreign is an indicator variable, we could generate the predicted variable with
one command:
. gen predprice2 = (1.05 + 0.05*foreign)*price
. list make foreign price predprice predprice2, nolabel
[ GSW ] 11 Creating new variables 99
make foreign
1. VW Rabbit Foreign
2. Olds 98 Domestic
3. Chev. Monza Domestic
4. Domestic
5. Datsun 510 Foreign
1. VW Rabbit Foreign F
2. Olds 98 Domestic D
3. Chev. Monza Domestic D
4. Domestic D
5. Datsun 510 Foreign F
. describe where
Variable Storage Display Value
name type format label Variable label
Stata has some useful tools for working with string variables. Here we split the make variable into
make and model and then create a variable that has the model together with where the model was man-
ufactured:
. gen model = usubstr(make, ustrpos(make," ")+1,.)
(1 missing value generated)
. gen modelwhere = model + " " + where
. list make where model modelwhere
There are a few things to note about how these commands work:
1. ustrpos(𝑠1 ,𝑠2 ) produces an integer equal to the first character in the string 𝑠1 at which the string
𝑠2 is found or 0 if it is not found. In this example, ustrpos(make,” ”) finds the position of the
first space in each observation of make.
2. usubstr(𝑠,𝑠𝑡𝑎𝑟𝑡,𝑙𝑒𝑛) produces a string of length 𝑙𝑒𝑛 characters, beginning at character 𝑠𝑡𝑎𝑟𝑡
of string 𝑠. If 𝑐1 = ., the result is the string from character 𝑠𝑡𝑎𝑟𝑡 to the end of string 𝑠.
3. Putting 1 and 2 together: usubstr(𝑠,ustrpos(𝑠,” ”)+1,.) will always give the string 𝑠 with
its first word removed. Because make contains both the make and the model of each car, and make
never contains a space in this dataset, we have found each car’s model.
4. The operator “+”, when applied to string variables, will concatenate the strings (that is, join them
together). The expression ”this” + ”that” results in the string ”thisthat”. When the variable
modelwhere was generated, a space (” ”) was added between the two strings.
5. The missing value for a string is nothing special—it is simply the empty string ””. Thus the value
of modelwhere for the car with no make or model is ” D” (note the leading space).
6. If your strings might contain Unicode characters, use the Unicode versions of the string functions,
as shown above. See [U] 12.4.2 Handling Unicode strings.
12 Deleting variables and observations
drop
The drop command is used to remove variables or observations from the dataset in memory.
• If you want to drop variables, use drop varlist.
• If you want to drop observations, use drop with an if or an in qualifier or both.
101
[ GSW ] 12 Deleting variables and observations 102
. drop in 1/3
(3 observations deleted)
. list
. drop gear_ratio
. list
. drop m*
. list
These changes are only to the data in memory. If you want to make the changes permanent, you need
to save the dataset.
[ GSW ] 12 Deleting variables and observations 103
keep
keep tells Stata to drop all variables except those specified explicitly or through the use of an if or
in expression. Just like drop, keep can be used with varlist or with qualifiers but not with both at once.
We use a clear command at the start of this example so that we can reload the afewcarslab dataset:
. clear
. use afewcarslab
(A few 1978 cars)
. list
. keep in 4/7
(3 observations deleted)
. list
. keep m*
. list
make mpg
1. Buick Regal 20
13 Using the Do-file Editor—automating Stata
If you ever forget what a button does, hover the mouse pointer over a button, and a tooltip will appear.
Open: Open a do-file from disk in a new tab in the Do-file Editor.
Paste: Paste the text from the Clipboard into the current document.
104
[ GSW ] 13 Using the Do-file Editor—automating Stata 105
Toggle bookmark: Turn on or off the bookmark on the current line. Bookmarks are a
way to move quickly within the do-file. They are quite useful in long do-files or when
debugging.
Show file in Viewer: Show the contents of the do-file in a Viewer window. This is worth-
while when editing files that contain SMCL tags, such as log files or help files.
Execute (do): Run the commands in the do-file, showing all commands and their output.
If text is highlighted, the button becomes the Execute selection (do) button and will run
only the selected lines, showing all output. We will refer to this as the Do button.
You will notice that the color of the text changes as you type. The different colors are examples of the
Do-file Editor’s syntax highlighting. The colors and text properties of the syntax elements can be changed
by selecting Edit > Preferences... from the Do-file Editor menu bar and then clicking on the Colors tab
in the resulting window. You can also define your own list of keywords for syntax highlighting.
Syntax highlighting extends beyond highlighting Stata commands. You can switch the syntax high-
lighting from Stata by going to the Language menu and choosing the language you would like. The
Language menu includes a selection for Markdown because Stata can process Markdown to create dy-
namic documents. See [RPT] dyndoc for more information. This menu also contains selections for
Python and Java because Stata has both Python integration and Java integration. See [P] PyStata inte-
gration and [P] Java integration for more information. Stata will default to the proper language based
on the extension of the file you are editing, but if the file has not been saved yet, you will need to tell it
what language to choose.
Also note that if you pause briefly as you type, the Do-file Editor will allow autocompletion of com-
mand names and words that are already in the do-file and, in StataNow, autocompletion of variable
names, macros, and stored results. Once the suggestions appear, more typing will narrow down the pos-
sibilities. You can navigate the suggestions using the up- and down-arrow keys or keep typing to narrow
them to a single word. Once you have the word you like, pressing Enter will place the word in your
do-file.
[ GSW ] 13 Using the Do-file Editor—automating Stata 107
Click on the Do button, , to execute the commands. Stata executes the commands in sequence,
and the results appear in the Results window:
. do C:\Users\Stata\AppData\Local\Temp\STD08000000.tmp
. * an example do-file
. sysuse auto
(1978 automobile data)
. generate gp100m = 100/mpg
. label var gp100m "Gallons per 100 miles"
. regress gp100m weight foreing
variable foreing not found
r(111);
.
end of do-file
The do ”C:\ . . .” command is how Stata executes the commands in the Do-file Editor. Stata saves
the commands from a do-file with unsaved changes to a temporary file and issues the do command to
execute them. Everything worked as planned until Stata saw the misspelled variable. The first three
commands were executed, but an error was produced on the fourth. Stata does not know of a variable
named foreing. We need to go back to the Do-file Editor and change the misspelled variable name to
foreign in the last line:
Click on the Do button again. Alas, Stata now fails on the first line—it will not overwrite the dataset
in memory that we changed.
. do C:\Users\Stata\AppData\Local\Temp\STD08000000.tmp
. * an example do-file
. sysuse auto
no; dataset in memory has changed since last saved
r(4);
.
end of do-file
[ GSW ] 13 Using the Do-file Editor—automating Stata 108
.
end of do-file
You might want to select File > Save as... to save this do-file from the Do-file Editor. Later, you could
select File > Open to open it and then add more commands as you move forward with your analysis. By
saving the commands of your analysis in a do-file as you go, you do not have to worry about retyping
them with each new Stata session. Think hard about removing the clear option from the first command.
After you have saved your do-file, you can execute the commands it contains by typing do filename,
where the filename is the name of your do-file.
Finally, you can create a New > Project... to keep track of collections of files used in a project. These
can be do-files, data files, graph files, or any other files you like. For more information on the Project
Manager, see [P] Project Manager.
When you select Edit > Find > Balance braces, the Do-file Editor looks to the left and right of the
current cursor position or selection and creates a selection that includes the narrowest level of matching
braces. If you select Balance braces again, the editor will expand the selection to include the next level
of matching braces. If there is no match, the cursor will not move. Balancing braces is useful for working
with blocks of code defined by loops or if commands. See [P] foreach, [P] forvalues, [P] while, and
[P] if for more information.
Balance braces is easier to explain with an example. Type {now {is the} time} in the Do-file
Editor. Place the cursor between the words is and the. Select Edit > Find > Balance braces. The
Do-file Editor will select {is the}. If you select Balance braces again, the Do-file Editor will select
{now {is the} time}.
Text in Stata strings can include Unicode characters and is encoded as UTF-8 (see [U] 12.4.2 Handling
Unicode strings). However, you may have do-files, ado-files, or other text files that you used with
Stata 13 or earlier, and those files contain characters other than plain ASCII such as accented characters,
Chinese, Japanese, or Korean (CJK) characters, Cyrillic characters, and the like. If you open a file that is
not encoded in UTF-8, Stata prompts you to specify the encoding for the file so that it can convert the file
to UTF-8. If you cancel the conversion or choose the wrong encoding, you can try the conversion again
later using Convert to UTF-8. The conversion to UTF-8 can be undone by using Edit > Undo and is not
permanent until you save the do-file. For Stata datasets with characters not encoded in UTF-8 or for bulk
conversion of multiple Stata files, you should use the unicode translate command.
Editing tip: You can click on the left margin near a line number to select the entire line and the end-
of-line characters. Doing so makes it easy to delete lines or cut lines and paste them elsewhere. You can
click and drag within the line-number column to select a range of complete lines.
You can increase the level of indentation for a permanent bookmark’s label in the Navigation Control
by adding # to the bookmark comment. For example, bookmark comment //## Bookmark 2 will be
indented one level more than bookmark comment //# Bookmark 1.
To delete all permanent and temporary bookmarks, use Edit > Delete all bookmarks....This will
remove all permanent bookmark lines, as well as remove all permanent and temporary bookmark icons
from the bookmark margin.
Projects
For advanced users managing many files as part of a project, Stata has a Project Manager that uses
the Do-file Editor. For more information on the Project Manager, see [P] Project Manager.
Auto backup
The Do-file Editor now creates a backup file whenever it opens a document or creates a new one.
When an existing document is opened, Stata creates a backup file of a document that is saved to disk
in the same directory using the existing document’s filename prefixed with ~ and with the extension
.stswp. When you edit a new and unsaved document, it saves the backup file to the temp directory.
When a document is closed, the backup file is deleted. However, if Stata does not exit cleanly because
of a power outage or a computer crash, the backup file is left behind.
By default, Stata periodically backs up the document every 4 seconds if an edit has been made or
after an addition or a deletion of 200 characters or more. The time interval can be changed in the Do-file
Editor’s advanced settings, and the backup feature can also be turned off.
[ GSW ] 13 Using the Do-file Editor—automating Stata 114
When you attempt to open a document in the Do-file Editor, it first checks for the existence of a
backup file. If a backup file is found, the Do-file Editor prompts you that a backup file exists and asks if
you want to recover the backup file, open the original document, or cancel. We will discuss the options
in reverse order. Choosing to cancel will cancel opening the document and leave the backup file on disk.
If you choose to open the original document, the original document is opened in the Do-file Editor, and
the backup file is deleted from disk. If you choose to recover the backup file, the backup file is opened as
a new and unsaved document in the Do-file Editor with its default filename set to the original filename
and the string Recovered appended to the filename. The backup file is deleted from disk. You can keep
the recovered document by saving it to disk either as a new file or by overwriting the original document,
or you can disregard the changes by closing the document without saving it.
115
[ GSW ] 14 Graphing data 116
Graph window
When the Graph window comes up, it shows our graph in a window with a toolbar. The first four
buttons are familiar to us from other Stata windows: Open, Save, Print, and Copy. The next two buttons
are new:
Rename graph: This button allows the graph to be renamed. Why would you do this? If
you would like to have multiple graphs open at once, the graphs need to be named. So you
can click on the Rename graph button to give a graph a name. This graph will then remain
open when you create your next graph.
Graph Editor: Stata has a Graph Editor that allows you to manipulate and edit your graph.
This feature will be introduced in the next chapter.
The inactive buttons to the right of the Graph Editor button are used by the Graph Editor, so their
meanings will become clear in the next chapter.
We decide that we like this graph and would like to save it. We can save it either by clicking on the
Save button and choosing a name and a location or by right-clicking on the Graph window itself and
selecting Save as....
Start the Editor by right-clicking on your graph and selecting Start Graph Editor. Click once on the
title of the graph. Here is a picture of the Graph Editor with its elements labeled.
Main menu Standard toolbar Contextual toolbar Selected object
Select any of the tools along the left of the Graph Editor window to edit the graph. The Pointer (Select
tool), , is selected by default.
You can change the properties of objects or drag them to new locations by using the Pointer. As
you select objects with the Pointer, a Contextual Toolbar will appear just above the graph. In the above
example, the title of the graph is selected, so the Contextual Toolbar has controls that are relevant for
editing titles. You can use any of the controls on the Contextual Toolbar to immediately change the
most important properties of the selected object. Right-click on an object to access more properties and
operations. Hold the Shift key when dragging objects to constrain the movement to horizontal or vertical
directions.
117
[ GSW ] 15 Editing graphs 118
Add text, lines, or markers (with optional labels) to your graph by using the three Add... tools— ,
, and . Lines can be changed to arrows by using the Contextual Toolbar. If you do not like the
default properties, simply change their settings in the Contextual Toolbar before adding the text, line, or
marker. The new settings will then be applied to all added objects, even in future Stata sessions.
Do not be afraid to try things. If you do not like a result, change it back by using the same tool or by
clicking on the Undo button, , in the Standard Toolbar for the Graph Editor (below the main menu).
Edit > Undo in the main menu does the same thing.
Remember to reselect the Pointer tool when you want to drag objects or change their properties.
You can move objects on the graph and have the rest of the objects adjust their position to accommo-
date the move with the Grid edit tool, . With this tool, you are repositioning objects in the underlying
grid that holds the objects in the graph. Some graphs, for example, by graphs, are composed of nested
grids. You can reposition objects only within the grid that contains them; they cannot be moved to other
grids.
You can also select objects in the Object Browser along the right of the graph. This window shows
a hierarchical listing of the objects in the graph. Clicking or right-clicking on an object in the Object
Browser is the same as clicking or right-clicking on the object in the graph.
The Graph Editor has the ability to record your actions and play them back on later graphs. When you
click on the Start recording button, , every editing action you take, including undos and redos, is
recorded. If you would like to do some editing that is not recorded, you can click on the Pause recording
button, . You can click on the Pause recording button again to resume recording. When you are done
with your recording, click on the Start recording button. You will be prompted to save your recording.
Any recording you save is available from the Play recording button, , and may be applied to future
graphs. You can even play a recording in any Stata graph command by using the play option. See
Graph Recorder in [G-1] Graph Editor for more information.
Stop the editor by selecting File > Stop Graph Editor from the main menu or by clicking on the
Graph Editor button. When you stop the Graph Editor, you will be prompted to save your graph if you
have made any changes. If you do not save your graph, your changes will not be lost, but you will risk
losing them if you create a new graph in the same Graph window. You must stop the Editor if you would
like to work on other tasks in Stata.
Here are a few of the things that you can do with the Editor:
• Add annotations using lines, arrows, and text.
• Add or remove grid lines or reference lines.
• Add or modify titles, captions, and notes.
• Change scatterplots to line plots, connected plots, areas, bars, spikes, or drop lines—and, of course,
vice versa.
• Change the size, color, margin, and other properties of your graph’s titles (or any other text on the
graph).
• Move your legend to another side of the graph, or even place it in the plot region.
• Change the aspect ratio of your graph.
• Stack the bars on a bar graph or turn them into percentages.
• Rotate or change the angle of axis labels.
• Add custom ticks and labels to the axes.
[ GSW ] 15 Editing graphs 119
• Change the rule for the number and spacing of ticks and labels on an axis.
• Emphasize a point on the graph, whether marker, bar, spike, or other plot, by making it a custom
color, size, or symbol.
• Change the text or properties of a marker label.
Because you can edit every property of every object on the graph, you can change almost anything
about your graph. To learn more, see [G-1] Graph Editor or type help graph editor.
16 Saving and printing results by using logs
Logging output
All the output that appears in the Results window can be captured in a log file. Stata can save the
file in one of two different formats. By default, Stata will save the file in its Stata Markup and Control
Language (SMCL) format, which preserves all the formatting and links from the Results window. You
can open these results in the Viewer, and they will behave as though they were in the Results window.
If you would rather have plain-text files without any formatting, you can save the file as a plain log file.
We recommend using the SMCL format because SMCL files can be translated into a variety of formats
readable by applications other than Stata with the File > Log > Translate... menu (see [R] translate).
To start a log file, click on the Log button, . This will open a standard file dialog that allows you
to specify a directory and filename for your log. If you do not specify a file extension, the extension
.smcl will be added to the filename. If you specify a file that already exists, you will be asked whether
you want to append the new log to the file or overwrite the file with the new log.
120
[ GSW ] 16 Saving and printing results by using logs 121
name: <unnamed>
log: C:\Users\stata\Documents\Stata\base.smcl
log type: smcl
opened on: 20 Nov 2024, 06:28:20
. sysuse auto
(1978 automobile data)
. by foreign, sort: summarize price mpg
price 1.0000
mpg -0.4686 1.0000
. log close
name: <unnamed>
log: C:\Users\stata\Documents\Stata\base.smcl
log type: smcl
closed on: 20 Nov 2024, 06:28:21
Printing logs
To print a standard SMCL log file, you need to have the log file open in a Viewer window. Once the
log file is in the Viewer, you can click on the Print button, right-click on the Viewer window, and select
Print..., or select File > Print.... A Print dialog will appear. After you click on Print, a Print settings
dialog will appear.
• You can fill in none of, any of, or all the items Header, Name, and Project. You can check or uncheck
options to Print line numbers, Print header, and Print logo. These items are saved and will appear
again in the print sheet Print settings (in this and in future Stata sessions).
• You can set the font, margins, and color scheme that the printer will use by clicking on Prefs... in
the Print settings dialog to open the Printer preferences dialog. Monochrome is for black-and-white
printing, Color is for default color printing, and Custom 1 and Custom 2 are for customized color
printing. You can set the font by clicking on the Font... button. The resulting Font dialog will list
only the fixed-width “typewriter” fonts (for example, Courier) available for your printer.
You could also use the translate command to generate a PostScript or PDF version of the log file.
See [R] translate for more information.
If your log file is a plain-text file (.log instead of .smcl), you can open it in a text editor, such as
Notepad, in the Do-file Editor or in your favorite word processor. You can then edit the log file—add
headings, comments, etc.—format it, and print it. If you bring the log file into a word processor, it will
be displayed and printed with its default font. The log file will not be easily readable when printed in a
proportionally spaced font (for example, Times New Roman or Georgia). It will look much better printed
in a fixed-width font (for example, Courier New).
You may wish to associate the .log extension with a text editor (such as Notepad or WordPad) in
Windows. You can then edit and print the logs from those Windows applications if you like.
[ GSW ] 16 Saving and printing results by using logs 123
If you start working and then wish you had started a cmdlog file, you can save yourself heartache
by saving the contents of the History window. The History window stores the last 5,000 commands you
have typed. Simply right-click on the History window and select Save all... from the menu. This will
work best if you first filter out all the commands that resulted in errors as was shown in The History
window in [GSW] 2 The Stata user interface. If you would like to move the commands directly to the
Do-file Editor, select Select all followed by Send selected to Do-file Editor. You may find this method
a more convenient way to create a text file containing only the commands that you typed during your
session.
See [GSW] 13 Using the Do-file Editor—automating Stata, [U] 16 Do-files, and [U] 15 Saving and
printing output—log files for more information.
17 Setting font and window preferences
Changing and saving fonts and sizes and positions of your windows
You may find that you would like to change the fonts and display style of Stata’s windows, depending
on your monitor resolution and personal preferences. At the same time, there could be requirements for
font usage, say, when you submit graphs to journals. Stata accommodates both of these by allowing sets
of preferences for how windows are displayed.
We will first cover what can be changed in each window and then talk about what you can manage
with your preferences.
Graph window
The preferences for the Graph window can be changed by right-clicking on the Graph window and
choosing Preferences... from the contextual menu. The settings can then be set for how graphs are
displayed in Stata. The settings that should be used when printing can be set under the Printer tab. The
behavior of the Clipboard is controlled under the Clipboard tab.
The Graph preferences allow different schemes that control the look of graphs. These schemes provide
a quick way to optimize graphs for printing or to display on a screen. There are even schemes defined for
The Economist and the Stata Journal so that you can get the details for these publications right without
much fuss. Changing the scheme does not change the current graph—it applies the settings to future
graphs.
124
[ GSW ] 17 Setting font and window preferences 125
The Results and Viewer windows have color schemes that control the display of input, text, results,
errors, links, and highlighted text. Each has its color scheme set in the same fashion: you can right-click
on the window and select or design your own color scheme. The default setting for both the Results
window and the Viewer is the built-in Standard scheme, which uses a white background and dark text.
There are other built-in schemes as well as slots for custom schemes. The settings for the Viewer affect
all Viewer windows at once. Choosing an overall scheme from the General tab will reset all custom
settings to the settings determined by that scheme.
126
[ GSW ] 18 Learning more about Stata 127
• Many supplementary books about Stata are available. Visit the Stata Bookstore at
https://www.stata.com/bookstore/.
• Take a Stata NetCourse○R . NetCourse 101 is an excellent choice for learning about Stata. See
https://www.stata.com/netcourse/ for course information and schedules.
• Attend a classroom or a web-based training course taught by StataCorp. Visit
https://www.stata.com/training/classroom-and-web/ for course information and schedules.
• View a webinar led by Stata developers. Visit https://www.stata.com/training/webinar/ for the
current list of topics and schedule.
• Watch Stata videos at https://www.youtube.com/user/statacorp.
Internet resources
The Stata website (https://www.stata.com) is a good place to get more information about Stata. You
will find answers to FAQs, ways to interact with other users, official Stata updates, and other useful
information. You can also join Statalist, a forum devoted to discussion of Stata and statistics.
You will also find information on Stata NetCourses○R , which are interactive courses offered over the
Internet that vary in length from a few weeks to eight weeks. Stata also offers in-person and web-based
training sessions, as well as webinars on Stata features. Visit https://www.stata.com/learn/ for more
information.
At the website is the Stata Bookstore, which contains books that we feel may be of interest to Stata
users. Each book has a brief description written by a member of our technical staff explaining why we
think this book may be of interest.
We suggest that you take a quick look at the Stata website now. You can register your copy of Stata
online and request a free subscription to the Stata News.
Visit https://www.stata-press.com for information on books, manuals, and journals published by Stata
Press. The datasets used in examples in the Stata manuals are available from the Stata Press website.
Also visit https://www.stata-journal.com to read about the Stata Journal, a quarterly publication con-
taining articles about statistics, data analysis, teaching methods, and effective use of Stata’s language.
Visit Stata’s official blog at https://blog.stata.com for news and advice related to the use of Stata. The
articles appearing in the blog are individually signed and are written by the same people who develop,
support, and sell Stata. The Stata Blog: Not Elsewhere Classified also has links to other blogs about
Stata, written by Stata users around the world.
Follow Stata on Facebook at https://facebook.com/statacorp, Twitter at https://twitter.com/stata, In-
stagram at https://www.instagram.com/statacorp, and LinkedIn at
https://www.linkedin.com/company/statacorp. You may also follow Stata on Twitter at
https://twitter.com/stata fr or https://twitter.com/stata es. These are good ways to stay up-to-the-
minute with the latest Stata information. Watch short example videos of using Stata on YouTube at
https://www.youtube.com/user/statacorp.
See [GSW] 19 Updating and extending Stata—Internet functionality for details on accessing offi-
cial Stata updates and free additions to Stata on the Stata website.
19 Updating and extending Stata—Internet functionality
Sorted by:
This functionality is everywhere in Stata. Any command that reads a file with a filename in its syntax
can use a web address as easily as a file that is stored on your computer.
This example used the HTTPS protocol for retrieving the file. Stata also understands the HTTP and FTP
protocols.
130
[ GSW ] 19 Updating and extending Stata—Internet functionality 131
The first entry points you to all the postestimation commands that are available after logistic re-
gression. The second entry points to Stata’s built-in estat gof command specifically for computing
goodness-of-fit statistics after logistic regression. You investigate this command and find it interesting.
You see that the next three links point to FAQs and examples on UCLA’s website. Then the next three links
are for articles in the SJ. You are interested in multinomial logistic regression, so you decide to check the
last of these links. It points to an article in the SJ, volume 12, number 3 (third quarter). You should click
on the st0269 link because it will go to the command associated with this article.
You will see that the package has one help file for the new commands. Click the
st0269/mlogitgof.sthlp link to see if the mlogitgof command looks interesting. If you decide
that you would like to install the command, click the Back button and click on the link click here to
install. If you decide that you would like to use some of the ancillary files—files that typically help
explain the workings of the command, you could download those, too. You do not need to worry—doing
so will not interfere in any way with your copy of Stata. We will show you how to safely uninstall these
commands shortly.
[ GSW ] 19 Updating and extending Stata—Internet functionality 135
You can keep the community-contributed commands you have installed up to date by using the ado
update command. Typing ado update will check for updates, while typing ado update, update will
check for updates and install any available updates.
Now suppose that you decide that you would like to uninstall the package. Doing so is simple enough:
select Help > SJ and community-contributed features, and click on the List link. You should see the
following:
[ GSW ] 19 Updating and extending Stata—Internet functionality 136
If you click on the one-line description of the package, you will see the full description of what has
been installed. Here is what you would see if you scroll to the bottom, with a different install date, of
course:
You can uninstall materials by clicking on click here to uninstall when you are looking at the
package description.
For information on downloading community-contributed commands by using the net command, see
[R] net.
A Troubleshooting Stata
Contents
A.1 If Stata does not start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
A.2 Troubleshooting tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
137
[ GSW ] A Troubleshooting Stata 138
StataSE
Target type: Application
Target location: Stata18
Target: C:\Program Files\Stata18\StataSE.exe /UseRegistryStartin
Start in:
Shortcut key: None
Run: Normal window
The field names may be slightly different, depending on the version of Windows that you are running.
The names and locations of files may vary from this. There are two things to pay attention to: the Target
and Start in fields. Target is the actual command that is executed to invoke Stata. Start in is the directory
to switch to before invoking the application. You can change these fields and then click on OK to save
the updated Properties Sheet.
You can have Stata start in any directory you desire. If necessary, delete the parameter
/UseRegistryStartin from your Target field. Then change the Start in field of Stata’s Properties
Sheet to the location you would like Stata to have as its default working directory. Of course, once Stata
is running, you can change directories whenever you wish by using File > Change working directory...;
see also [D] cd.
139
[ GSW ] B Advanced Stata usage 140
When you invoke Stata, the usual opening appears but with the following additional command, which
will be executed:
running C:\Users\mydir\profile.do ...
How does the command work? Let’s work from the inside out:
• c(current date) and c(current time) are local system macros containing the current date
and current time. See [P] creturn for more information.
• The left (‘) and right (’) quotes around the local macros expand them. See [P] macro for a full
explanation.
• The Clock() function uses the resulting date string and the date mask ”DMYhms” to create a date-
time number Stata understands. See [D] Datetime.
[ GSW ] B Advanced Stata usage 141
See [U] 16 Do-files for an explanation of do-files. They are nothing more than text files containing
sequences of commands for Stata to execute.
Parameter Result
/b set background (batch) mode and log in plain text
/e set background (batch) mode and log in plain text without prompting when
Stata command has completed
/q suppress logo and initialization messages
/rngstream# set random-number generator to mt64s (see [R] set rng) and set
random-number stream to # (see [R] set rngstream)
/s set background (batch) mode and log in SMCL
/i suppress Stata application icon in the Windows taskbar
Method 2
If you would like to have a batch job that you could run at a particular time or that you could save for
later use, you can use the Task Scheduler, which is part of most Windows installations.
This is a bit more advanced, and its implementation differs slightly for each kind of Windows, but
here is the general gist.
In Windows 11 and Windows 10, you can search for the Task Scheduler in the search box in the
taskbar. Once you have opened the Task Scheduler, click on Create Basic Task, and follow the steps of
the Basic Task Schedule Wizard to schedule a do-file to run in batch mode. You must specify the /b or
/e option. In the Start in field, type the path where you would like the log file to be saved. When this
file runs, all output will be suppressed and written to a log file that will be saved in the path specified.
[ GSW ] B Advanced Stata usage 143
General notes
While your do-file is executing, the Stata icon will appear on the taskbar.
If you click on the icon on the taskbar, Stata will display a box asking if you want to cancel the batch
job.
Once the do-file is complete, Stata will flash the icon on the taskbar on and off. You can then click
on the icon to close Stata. If you wish for Stata to automatically exit after running the batch do-file, use
/e rather than /b.
You do not have to run large do-files in batch mode. Any do-file that you run in batch mode can also
be run interactively. Simply start Stata, type log using filename, and type do filename. You can then
watch the do-file run, or you can minimize Stata while the do-file is running.
To change it back to match the locale set for your operating system, type
set locale_ui default
For a complete explanation of locales and Stata, see [U] 12.4.2.4 Locales in Unicode.
[ GSW ] B Advanced Stata usage 144
B.8 More
If you would like Stata to pause every time the screen fills with results, type set more on. This will
cause a more prompt to appear at the bottom of the Results window whenever there is more information
to be displayed than can fit on the screen. This happens, for example, when you are listing many
observations.
. list make mpg
make mpg
1. Linc. Continental 12
2. Linc. Mark V 12
3. Cad. Deville 14
4. Cad. Eldorado 14
5. Linc. Versailles 14
6. Merc. Cougar 14
7. Merc. XR-7 14
8. Peugeot 604 14
9. Buick Electra 15
10. Merc. Marquis 15
more
If you want to see the next screen of text, you have a few options: press any key, such as the Spacebar;
click on the More button, ; or click on the more link at the bottom of the Results window. To see
just the next line of text, press Enter. Pressing q will interrupt the command. If you click on the arrow of
the More button, you can also select the Run to completion menu item to let the command completely
finish.
145
[ GSW ] C More on Stata for Windows 146
A commands,
compared with menus, 23
ado command, 39
downloading and installing, 132–134
arithmetic operators, see operators, arithmetic
learning, 31, 126–128
auto hide, see windows, Auto Hide
uninstalling, 135
automatic update checking, see updates, automatic update
updating, 135
checking
comments, see log files, commenting
contextual menu, 25
B Graph window, 116
batch mode, 142–143 History window, 29
blog, see Stata Blog Variables window, 28
Break button, see button, Break Viewer window, 38
browse command, 2, 7, 63 copy, 49
button, copying,
Break, 26, 93 data, see data, copying
Clear more condition, 26, 144 graphs, see graphs, copying
Data Browser, 49 text, see text, copying
Data Editor (Browse), 2, 26 correlate command, 16, 17
Data Editor (Edit), 26, 49, 51 creating variables, see variables, creating
Do, see button, Execute (do)
Do-file Editor, 26, 105 D
Execute (do), 105, 107, 111 data,
Execute Quietly (run), 111 copying, 55, 72
Graph, 18, 26 tips, 56
Graph Editor, 116 entering, 51–56
Log, 24, 26, 120 graphing, 17–22
Open, 26 importing, see importing data
Print, 26 labeling, 77–82
Reset, 21 listing, 83–93
Run, see button, Execute (do) pasting, 55, 72
Save, 26 sorting, 56
Show file in Viewer, 112 strings, 53
Variables Manager, 26 Data Editor, 2–3, 49–63
Viewer, 26, 35 attach value labels, 57
by() option, 15, 19 browse mode, 63
by prefix, 14, 16, 23 toolbar button, 26
by/if/in tab, 7, 13, 16, 31 cell navigation, 53
color coding, 3, 52
C contextual menus, 50
create value labels, 57
cd command, 32, 139 creating variables, 58
clear command, 101 cursor location box, 51–53, 56
clear more condition, see more condition date
cmdlog command, 123 editing, 60–61
codebook command, 5–6, 42 formats, 60
command history, 27 mask, 60–61
command syntax, 23–24 edit mode toolbar button, 26
Command window, 25, 27, 32 edit within cell, 53
147
[ GSW ] Subject index 148
J M
Java, 106
Markdown, 106
jdbc command, 73, 76
median, 9
K
memory, 6, 47, 78, 144
allocation, 144
keep command, 101, 103 menus, 30
compared with commands, 23
L examples, see examples, menus
label command, 79 missing() function, 8, 63, 87
label data command, 79 missing values, 3, 5, 7, 11, 52, 56, 57, 86–87, 95–96, 100
label define command, 80, 82 extended, 57
label values command, 79, 82 more condition, 26, 144
label variable command, 22, 79
labels, N
data, 3, 79–80
net command, 39
value, 3–6, 10, 14, 79, 81–82
NetCourses, see Stata NetCourses
confusing with string variable, 89
network installation, 146
why needed, 6
nondocking windows, see windows, nondocking
variable, 4, 5, 79
Not Elsewhere Classified, see Stata Blog
license, 146
notes,
changing, 146
managing, 69–71
line, twoway subcommand, 21
timestamps, 71
linear regression, see regression, linear
numlist, 91
linking windows, see windows, linking
list command, 8, 74, 83–93
listing data, see data, listing O
loading data, see dataset, opening observations, 2
locale, 143 ODBC, 76
Log button, 26 odbc command, 73, 76
log command, 120–122 one-way table, see table, one-way
log files, 24, 120–123 open, 49
appending, 120 Open toolbar button, 26
commenting, 121 opening a dataset, see dataset, opening
contents, 121
[ GSW ] Subject index 151
Stata (continued) U
updating, see updates
Unicode, 100
user interface, see GUI
update command, 39
videos, 45, 127
updates, 39, 131
webinars, 127
automatic update checking, 131
won’t start, see troubleshooting
troubleshooting, 131
working directory, see working directory
use command, 47
Stata Journal, 40–41, 45–46, 126, 129, 130
User’s Guide, 126
Stata News, 129
ustrpos() function, 100
status bar, 32
usubstr() function, 100
storage type, see variables, data type
string variable, see variables, string
strings, 99–100 V
quoting, 89 value labels, creating and managing, 57
summarize command, 4, 8, 13–14, 24 variables, 2
SVG, 146 allowable names, 54
syntax, 42, 83–93 creating, 58, 94–100
abbreviations, 83 data type, 4–6, 78, 95
diagram, 24 display format, 4, 78
list command, 83 dropping, 28, 69, 101–102
syntax note, 2, 5, 7–19, 23 formatting, 53
sysprofile.do, 141 generating, see variables, creating
sysuse command, 1 keeping, 28, 69
missing values, see missing values
T name, 4, 78
abbreviating, 74, 84–86
tabbing windows, see windows, tabbing
autocompletion, 27
table,
naming, 53
one-way, 10–11, 14
renaming, 53
two-way, 11–12
storage type, see variables, data type
tabs, 29
string, 4, 5, 99–100
tabulate command, 11–14
confusing with value labels, 89
text,
Unicode, 78
copying, 104
text, see variables, string
pasting, 104
Variables Manager, 68–71
text variables, see variables, string
contextual menus, 69
TIFF, 146
creating varlists, 69
toolbar, main, 26
dropping variables, 69
tooltip, 26
filtering variables, 68
tostring command, 94
keeping variables, 69
training, see Stata training
notes, 69–71
troubleshooting, 137–138
printing, 69
unexpected problem, 138
restore columns, 69
updates, see updates, troubleshooting
restore sort order, 68
ttest command, 15
right-clicking, 69
Twitter, 126, 129
toolbar button, 26
twoway line command, 21
Variable pane, 68–69
twoway scatter command, 17–19, 21
Variable Properties pane, 70
two-way table, see table, two-way
type command, 39
[ GSW ] Subject index 153
W
webinars, see Stata videos
windows, 25
Auto Hide, 34
closing, 125
docking, 32–33
docking guides, 33–34
fine control, 32
linked, 32
linking, 32–34
moving, 33
nondocking, 32, 34