1y ago

74 Views

2 Downloads

8.81 MB

401 Pages

Transcription

STATA USER’S GUIDERELEASE 16 A Stata Press PublicationStataCorp LLCCollege Station, Texas

Copyright c 1985–2019 StataCorp LLCAll rights reservedVersion 16Published by Stata Press, 4905 Lakeway Drive, College Station, Texas 77845Typeset in TEXISBN-10: 1-59718-301-6ISBN-13: 978-1-59718-301-7This manual is protected by copyright. All rights are reserved. No part of this manual may be reproduced, storedin a retrieval system, or transcribed, in any form or by any means—electronic, mechanical, photocopy, recording, orotherwise—without the prior written permission of StataCorp LLC unless permitted subject to the terms and conditionsof 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, butnot limited to, the implied warranties of merchantability and fitness for a particular purpose. StataCorp may makeimprovements and/or changes in the product(s) and the program(s) described in this manual at any time and withoutnotice.The software described in this manual is furnished under a license agreement or nondisclosure agreement. The softwaremay be copied only in accordance with the terms of the agreement. It is against the law to copy the software ontoDVD, 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 c 1979 by Consumers Union of U.S.,Inc., Yonkers, NY 10703-1057 and is reproduced by permission from CONSUMER REPORTS, April 1979.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.NetCourseNow is a trademark 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.The suggested citation for this software isStataCorp. 2019. Stata: Release 16 . Statistical Software. College Station, TX: StataCorp LLC.

ContentsStata basics1Read this—it will help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22A brief description of Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93Resources for learning and using Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134Stata’s help and search facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245Flavors of Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316Managing memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347–more– conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .368Error messages and return codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379The Break key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4010Keyboard use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42Elements of Stata11Language syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4912Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8413Functions and expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12314Matrix expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14415Saving and printing output—log files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15916Do-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16517Ado-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17818Programming Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18319Immediate commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23720Estimation and postestimation commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24121Creating reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304Advice22Entering and importing data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30823Combining datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317i

iiContents24Working with strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31925Working with dates and times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32326Working with categorical data and factor variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33227Overview of Stata estimation commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35028Commands everyone should know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38329Using the Internet to keep up to date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .385Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .392Subject and author index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397

Stata basics1Read this—it will help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22A brief description of Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93Resources for learning and using Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134Stata’s help and search facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245Flavors of Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316Managing memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347–more– conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .368Error messages and return codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379The Break key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4010Keyboard use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421

1Read this—it will helpContents1.11.21.31.4Getting Started with Stata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The User’s Guide and the Reference manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.1PDF manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.1.1Video example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.2Example datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.2.1Video example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.3Cross-referencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.4The index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.5The subject table of contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.6Typography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.7Vignette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What’s new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24445566667788

[ U ] 1 Read this—it will helpThe Stata Documentation consists of the following [PSS][P][RPT][SP][SEM][SVY][ST][TS][TE][I]Getting Started with Stata for MacGetting Started with Stata for UnixGetting Started with Stata for WindowsStata User’s GuideStata Base Reference ManualStata Bayesian Analysis Reference ManualStata Choice Models Reference ManualStata Data Management Reference ManualStata Dynamic Stochastic General Equilibrium Models Reference ManualStata Extended Regression Models Reference ManualStata Finite Mixture Models Reference ManualStata Functions Reference ManualStata Graphics Reference ManualStata Item Response Theory Reference ManualStata Lasso Reference ManualStata Longitudinal-Data/Panel-Data Reference ManualStata Meta-Analysis Reference ManualStata Multilevel Mixed-Effects Reference ManualStata Multiple-Imputation Reference ManualStata Multivariate Statistics Reference ManualStata Power, Precision, and Sample-Size Reference ManualStata Programming Reference ManualStata Reporting Reference ManualStata Spatial Autoregressive Models Reference ManualStata Structural Equation Modeling Reference ManualStata Survey Data Reference ManualStata Survival Analysis Reference ManualStata Time-Series Reference ManualStata Treatment-Effects Reference Manual:Potential Outcomes/Counterfactual OutcomesStata Glossary and Index[M]Mata Reference ManualIn addition, installation instructions may be found in the Installation Guide.3

41.1[ U ] 1 Read this—it will helpGetting Started with StataThere are three Getting Started manuals:[GSM] Getting Started with Stata for Mac[GSU] Getting Started with Stata for Unix[GSW] Getting Started with Stata for Windows1. Learn how to use Stata — read the Getting Started (GSM, GSU, or GSW) manual.2. Now turn to the other manuals; see [U] 1.2 The User’s Guide and the Reference manuals.1.2The User’s Guide and the Reference manualsThe User’s Guide is divided into three sections: Stata basics, Elements of Stata, and Advice. Thetable of contents lists the chapters within each of these sections. Click on the chapter titles to see thedetailed contents of each chapter.The Guide is full of a lot of useful information about Stata; we recommend that you read it. Ifyou only have time, however, to read one or two chapters, then read [U] 11 Language syntax and[U] 12 Data.The other manuals are the Reference manuals. The Stata Reference manuals are each arrangedlike an encyclopedia—alphabetically. Look at the Base Reference Manual. Look under the name ofa command. If you do not find the command, look in the subject index in [I] Stata Glossary andIndex. A few commands are so closely related that they are documented together, such as ranksumand median, which are both documented in [R] ranksum.Not all the entries in the Base Reference Manual are Stata commands; some contain technicalinformation, such as [R] Maximize, which details Stata’s iterative maximization process, or [R] Errormessages, which provides information on error messages and return codes.Like an encyclopedia, the Reference manuals are not designed to be read from cover to cover.When you want to know what a command does, complete with all the details, qualifications, andpitfalls, or when a command produces an unexpected result, read its description. Each entry is writtenat the level of the command. The descriptions assume that you have little knowledge of Stata’sfeatures when they are explaining simple commands, such as those for using and saving data. Formore complicated commands, they assume that you have a firm grasp of Stata’s other features.If a Stata command is not in the Base Reference Manual, you can find it in one of the otherReference manuals. The titles of the manuals indicate the types of commands that they contain. TheProgramming Reference Manual, however, contains commands not only for programming Stata butalso for manipulating matrices (not to be confused with the matrix programming language describedin the Mata Reference Manual).1.2.1PDF manualsEvery copy of Stata comes with Stata’s complete PDF documentation.The PDF documentation may be accessed from within Stata by selecting Help PDF documentation.Even more convenient, every help file in Stata links to the equivalent manual entry. If you are readinghelp regress, simply click on (View complete PDF manual entry) below the title of the help fileto go directly to the [R] regress manual entry.We provide some tips for viewing Stata’s PDF documentation at ocumentation-tips/.

[ U ] 1 Read this—it will help1.2.1.15Video examplePDF documentation in Stata1.2.2Example datasetsVarious examples in this manual use what is referred to as the automobile dataset, auto.dta. Wehave created a dataset on the prices, mileages, weights, and other characteristics of 74 automobilesand have saved it in a file called auto.dta. (These data originally came from the April 1979 issueof Consumer Reports and from the United States Government EPA statistics on fuel consumption;they were compiled and published by Chambers et al. [1983].)In our examples, you will often see us type. use https://www.stata-press.com/data/r16/autoWe include the auto.dta file with Stata. If you want to use it from your own computer rather thanvia the Internet, you can type. sysuse autoSee [D] sysuse.You can also access auto.dta by selecting File Example datasets., clicking on Exampledatasets installed with Stata, and clicking on use beside the auto.dta filename.There are many other example datasets that ship with Stata or are available over the web. Here isa partial list of the example datasets included with eexp.dtauslifeexp2.dtavoter.dtaxtline1.dta1978 Automobile Data1978 Automobile DataSubset of 1978 Automobile Datafictional blood pressure datafictional blood pressure dataPatient Survival in Drug Trial1980 Census data by stateCity Temperature DataCity Temperature DataEducation and GDPU.S. GNP, 1967–2002Life expectancy, 1998fictional network diagram datafictional network diagram dataU.S. National Longitudinal Study of Young Women (NLSW, 1988 extract)U.S. National Longitudinal Study of Young Women (NLSW, 1988 extract)U.S. Census, 2000, extractSubsea elevation of Lamont sandstone in an area of OhioS&P 500NOAA Sea Surface Temperaturesimulated time-series datafictional data on calories consumedU.S. life expectancy, 1900–1999U.S. life expectancy, 1900–19401992 presidential voter datafictional data on calories consumed

6[ U ] 1 Read this—it will helpAll of these datasets may be used or described from the Example datasets. menu listing.Even more example datasets, including most of the datasets used in the reference manuals, areavailable at the Stata Press website (https://www.stata-press.com/data/). You can download the datasetswith your browser, or you can use them directly from the Stata command line:. use https://www.stata-press.com/data/r16/nlsworkAn alternative to the use command for these example datasets is webuse. For example, typing. webuse nlsworkis equivalent to the above use command. For more information, see [D] webuse.1.2.2.1Video exampleExample datasets included with Stata 161.2.3Cross-referencingThe Getting Started manual, the User’s Guide, and the Reference manuals cross-reference eachother.[R] regress[D] reshape[XT] xtregThe first is a reference to the regress entry in the Base Reference Manual, the second is a referenceto the reshape entry in the Data Management Reference Manual, and the third is a reference to thextreg entry in the Longitudinal-Data/Panel-Data Reference Manual.[GSW] B Advanced Stata usage[GSM] B Advanced Stata usage[GSU] B Advanced Stata usageare instructions to see the appropriate section of the Getting Started with Stata for Windows, GettingStarted with Stata for Mac, or Getting Started with Stata for Unix manual.1.2.4The indexThe Glossary and Index contains a combined index for all the manuals.To find information and commands quickly, you can use Stata’s search command; see [R] search.At the Stata command prompt, type search geometric mean. search searches Stata’s keyworddatabase and the Internet to find more commands and extensions for Stata written by Stata users.1.2.5The subject table of contentsA subject table of contents for the User’s Guide and all the Reference manuals except the MataReference Manual is located in the Glossary and Index. This subject table of contents may also beaccessed by clicking on Contents in the PDF bookmarks.

[ U ] 1 Read this—it will help1.2.67TypographyWe mix the ordinary typeface that you are reading now with a typewriter-style typeface that lookslike this. When something is printed in the typewriter-style typeface, it means that something is acommand or an option — it is something that Stata understands and something that you might actuallytype into your computer. Differences in typeface are important. If a sentence reads, “You could listthe result . . . ”, it is just an English sentence — you could list the result, but the sentence providesno clue as to how you might actually do that. On the other hand, if the sentence reads, “You couldlist the result . . . ”, it is telling you much more — you could list the result, and you could do thatby using the list command.We will occasionally lapse into periods of inordinate cuteness and write, “We described the dataand then listed the data.” You get the idea. describe and list are Stata commands. We purposelybegan the previous sentence with a lowercase letter. Because describe is a Stata command, it mustbe typed in lowercase letters. The ordinary rules of capitalization are temporarily suspended in favorof preciseness.We also mix in words printed in italic type, such as “To perform the rank-sum test, type ranksumvarname, by(groupvar)”. Italicized words are not supposed to be typed; instead, you are to substituteanother word for them.We would also like users to note our rule for punctuation of quotes. We follow a rule that is oftenused in mathematics books and British literature. The punctuation mark at the end of the quote isincluded in the quote only if it is a part of the quote. For instance, the pleased Stata user said shethought that Stata was a “very powerful program”. Another user simply said, “I love Stata.”In this manual, however, there is little dialogue,

The Getting Started manual, the User’s Guide, and the Reference manuals cross-reference each other. [R] regress [D] reshape [XT] xtreg The ﬁrst is a reference to the regressentry in the Base Reference Manual, the second is a reference to the reshapeentry in the Data Management Reference Manual, and the third is a reference to the

Related Documents: