Example: quiz answers

Do-files - Stata

And blank lines in lines in handling in the output of more other and running and running do-files for and running do-files for and running do-files for with DescriptionRather than typing commands at the keyboard, you can create a text file containing commandsand instruct Stata to execute the commands stored in that file. Such files are calleddo-filesbecausethe command that causes them to be executed do-file is a standard text file that is executed by Stata when you typedofilename.

old do-file written for Stata 2.1 that analyzed weighted data and did not have version 2.1 at the top, you would find that today’s Stata would flag some of the file’s lines as syntax errors. If …

Tags:

  Data, Do files, files

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Do-files - Stata

1 And blank lines in lines in handling in the output of more other and running and running do-files for and running do-files for and running do-files for with DescriptionRather than typing commands at the keyboard, you can create a text file containing commandsand instruct Stata to execute the commands stored in that file. Such files are calleddo-filesbecausethe command that causes them to be executed do-file is a standard text file that is executed by Stata when you typedofilename.

2 You canuse any text editor or the built-in Do-file Editor to create do-files; see[GSW] 13 Using the Do-fileEditor automating Stata . Using do-files rather than typing commands with the keyboard or usingdialog boxes offers several advantages. By writing the steps you take to manage and analyze yourdata in the form of a do-file, you can reproduce your work later. Also, writing a do-file makesthe inevitable debugging process much easier. If you decide to change one part of your analysis,changing the relevant commands in your do-file is much easier than having to start back at squareone, as is often necessary when working interactively.

3 In this chapter, we describe the mechanics ofdo-files. Long (2009) cogently argues that do-files should be used in all research projects and offersan abundance of time-tested advice in how to manage data and statistical 1 You can use do-files to create a batchlike environment in which you place all the commands youwant to perform in a file and then instruct Stata todothat file. Assume that you use your text editoror word processor to create a file contains these three lines:begin regionsummarize marriage_rate divorce_rate median_age if state!

4 ="Nevada"end [ U ] 16 Do-filesYou then enter Stata and instruct Stata todothe file:. do myjob. use (1980 Census data by state). tabulate regionCensusregionFreq. Percent Cntrl12 summarize marriage_rate divorce_rate median_age if state !="Nevada"VariableObs Mean Std. Dev. Min Maxmarriage_r~e49 .0106791 .0021746 .0074654 .0172704divorce_rate49 .0054268 .0015104 .0029436 .008752median_age49 typed onlydo myjobto produce this output.

5 Because you did not specify the file extension, Stata assumed you meantdo ; see[U] Filenaming VersionWe recommend that the first line in your do-file declare the Stata release you used when you wrotethe do-file; read better asbegin 13use regionsummarize marriage_rate divorce_rate median_age if state!="Nevada"end admit that we do not always follow our own advice, as you will see many examples in thismanual that do not include theversion you intend to keep the do-file, however, you should include this line because it ensures that yourdo-file will continue to work with future versions of Stata .

6 Stata is under continual development, andsometimes things change in surprising instance, in Stata , a new syntax for specifying the weights was introduced. If you had anold do-file written for Stata that analyzed weighted data and did not haveversion thetop, you would find that today s Stata would flag some of the file s lines as syntax errors. If you hadtheversion , it would work just as it used ahead to Stata 10, we introducedxtsetand declared that, to use thextcommands, youmustxtsetyour data first. Previously, you specified options on the end of eachxtcommand thatidentified the group and, optionally, the time variables.

7 Despite this change, if you includeversion 9or earlier at the top of your do-file, thextcommands will continue to work the old an overview of versioning and an up-to-date list of the issues that versioning does not addressautomatically, seehelp version.[ U ] 16 Do-files 3 When running an old do-file that includes aversionstatement, you need not worry about settingthe version back after it has completed. Stata automatically restores the previous value ofversionwhen the do-file Comments and blank lines in do-filesYou may freely include blank lines in your do-file.

8 In the previous example, the do-file could justas well have readbegin 13use regionsummarize marriage_rate divorce_rate median_age if state!="Nevada"end are four ways to include comments in a Begin the line with a * ; Stata ignores such lines.*cannot be used within Place the comment in/* * Place the comment after two forward slashes, that is,//. Everything after the//to the end ofthe current line is considered a comment (unless the//is part ofhttp://..).4. Place the comment after three forward slashes, that is,///.

9 Everything after the///to theend of the current line is considered a comment. However, when you use///, the next linejoins with the current you split long lines across multiple lines in the noteThe/* */,//, and///comment indicators can be used in do-files and ado-files only; you maynot use them interactively. You can, however, use the * comment indicator might readbegin * a sample analysis jobversion 13use * obtain the summary statistics: */tabulate regionsummarize marriage_rate divorce_rate median_age if state!

10 ="Nevada"end equivalently,begin a sample analysis jobversion 13use obtain the summary statistics:tabulate regionsummarize marriage_rate divorce_rate median_age if state!="Nevada"end style of comment indicator you use is up to you. One advantage of the/* */method is that itcan be put at the end of lines:4 [ U ] 16 Do-filesbegin * a sample analysis jobversion 13use region /* obtain summary statistics */summarize marriage_rate divorce_rate median_age if state!="Nevada"end fact,/* */can be put anywhere, even in the middle of a line:begin * a sample analysis jobversion 13use /* confirm this is latest */ region /* obtain summary statistics */summarize marriage_rate divorce_rate median_age if state!


Related search queries