UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Class Notes
I log, I do


1.0 Stata commands in this unit

. log using filename.log
log close
log off
log on
type
do

2.0 Demonstration and Explanation

2.1 The log Command, Example 1

. log using summary.log
use hsb2

generate lang = read + write
summarize read write lang
log close
type summary.log

2.2 The log Command, Example 2

. 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

2.3 Using a do-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.

2.4 A better do-file

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

4.0 Can you answer these questions?

  1. Can you create a do-file that does the following:
    a. computes frequencies for female and ses;
    b. computes descriptive statatistics for read, write and math;
    c. computes correlations between read, write, and math?

5.0 For More Information

6.0 Web Notes

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


How to cite this page

Report an error on this page

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


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.