|
|
|
||||
|
|
|||||
. log using filename.log
log close
log off
log on
type
do
. log using summary.log
use hsb2
generate lang = read + write
summarize read write lang
log close
type summary.log
The command log using summary.log opens a log file called summary.log that records everything you type and all of the output from the commands as a text file.
The command log close closes and saves the current log file.
The command type displays the contents of a file to the screen.
. log using resid.log
use hsb2
regress read write math science
rvfplot
predict r, rstudent
sort r
log off
list r
log on
list if abs(r) > 2.5
log close
type resid.log
This set of commands is much like the ones before, except that we use log off to temporarily suspend the log, and log on to resume the log. As before, we finish with log close to close and save the current log file.
Sometimes you may want to use the same commands on more than one file but you don't want to have to type them in more than once. Other times its easier to collect all of your Stata commands together in one place and do all at once rather than one at a time. A do-file allows you to place commands in a file and run them all at once. Any command that you can type in on the command line can be placed in a do-file.
Do-files are created with the do-file editor or any other text editor. Any command which can be executed from the command line can be placed in a do-file. Here are some commands that could be placed in a do-file:
set more off
use hsb2, clear
generate lang = read + write
label variable lang "language score"
tabulate lang
tabulate lang female
tabulate lang prog
tabulate lang schtyp
summarize lang, detail
table female, contents(n lang mean lang sd lang)
table prog, contents(n lang mean lang sd lang)
table ses, contents(n lang mean lang sd lang)
correlate lang math science socst
regress lang math science female
set more on
Let's look at a do-file that contains these commands that is on our floppy disk.
. type hsbbatch.do
Now let's "do" the file hsbbatch.do
. do hsbbatch
Notice that all of the commands scrolled off of the screen without prompting you with "-more-". This is because we started the do-file with set more off.
The above do-file, hsbbatch did not save the results. Let's improve it so it makes a log of its results. The additions are shown in italics. Notice we start with capture log close to close the log (in case it was open) and then the log using command starts logging our results to hsbbatch.log.
capture log close
log using hsbbatch.log, replace
set more off
use hsb2, clear
generate lang = read + write
label variable lang "language score"
tabulate lang
tabulate lang female
tabulate lang prog
tabulate lang schtyp
summarize lang, detail
table female, contents(n lang mean lang sd lang)
table prog, contents(n lang mean lang sd lang)
table ses, contents(n lang mean lang sd lang)
correlate lang math science socst
regress lang math science female
set more on
log close
Now let's "do" the file hsbbatch.do
. do hsbbatch
If we like, we could "run" the file hsbbatch.do and it would not show the results.
. run hsbbatch
Either way, we can see the results with the type command, i.e,
. type hsbbatch.log
The Stata Class Notes are available on the World Wide Web by visiting ...
http://www.ats.ucla.edu/stat/stata/notes/
The datasets schdat.dta and hsb2.dta can be loaded directly into Stata, over the
Internet, using the following command:
use http://www.ats.ucla.edu/stat/stata/notes/hsb2
UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services