Econometrics Helpdesk

Professor Lesley Chiou | Yinbo Gao (Stata Mentor)

Essentials

1. Once you open Stata, you should see this old school interface.

2. Download or send your data sets to a dedicated folder. You can download most sample datasets here.

3.1 In a Mac, create your workspace folder locally, then right click the folder, copy the path.

3.2 In a PC, create your workspace folder, then right click for Properties: Location to find the full pathname.

4. In the Stata command window, type “cd” (no parentheses), followed by pasting the folder address.

1. Go to File -> New -> Do-File (Or hit command+N). Open up a new do-file, and save this as “example.do”. Note: for convenience, please save your do file in your project folder where all of your projects and datasets are at. Following the format from the snip below.

2. The first line opens a log-file that will record all of the Stata output that follows. The command “set more off” makes sure that you don’t need to hit Enter if your output goes beyond the screen. The last line closes the log.

Now run your do-file:

do example.do

Note that if you have an error in your do-file and you attempt to run it again, you must first close the log. Type “log close.”

Now that you know how to run a do-file, let’s create your own do-file. Go to the Do-file Editor and select File: New. This should open up a new do-file, and save this as example.do. Go ahead and type in all of your commands from the data1.csv exercise that you did previously.

Be sure that your do-file example.do contains the following:

log using example.log, replace
set more off

[Your code goes here]

set more on
log close

 

The first line opens a log-file that will record all of the Stata output that follows. The command “set more off” makes sure that you don’t need to hit Enter if your output goes beyond the screen. The last line closes the log.

Now run your do-file:

do example.do

Note that if you have an error in your do-file and you attempt to run it again, you must first close the log. Type “log close.”

Go to the Command Window type the following commands:

You will see all of the saved variables from the dta file.

In the Variables Window, you will see four variables listed: finaid, parent, hsrank, and male. To run the regression, type the following in the command window and press enter:

regress – represents Ordinary Least Squares
finaid – the dependent variable
parent and hsrank – the independent variables

Compare your computer results with those in your textbook. If your results match those in your book, congratulations, you are well on your way to becoming a full-fledged econometrician.

We will now create a data file of our own. For practice, let us start a cross sectional regression of our own with 5 observations. To enter the data, we will use Excel to create a “raw” file of the data, which we will then read into Stata.

Open Excel. Enter in the following data:

x y
1 39
-6 20
8 50
-2 35
3 42

 

Next, in Excel, go to File:Save As, and type “data1” for Filename and choose “CSV (comma delimited)(*.csv)” for “Save as type.” Be sure to save this file data1.csv under your Econ272 folder.

In Stata’s Command Window, type:

insheet using data1.csv, name

insheet — reads ASCII (text) data created by spreadsheets such as Excel
using – tells Stata which dataset to use
data1.csv –name of the file to be read into Stata
name –tells Stata that the first row of the file contains the variable names
Next, run a regression with y as the dependent variable and x as the independent variable. You should get the following in your Results Window:

. regress y x

 

To save your data, type in the Command Window:

save data1.dta, replace

save –tells Stata to save the file
replace –tells Stata to replace an existing version of the file data1.dta (if it exists).

You can also list all the observations in your data by typing:

list

list –tells Stata to list all the observations in the current dataset.

Q&A

I document most frequently asked questions here in case you need them. If you can’t make to the office hour, you may also submit your question down below.

Think Do file as a mini software you are writing. You don’t want your coworker to retype all the commands just to see your data analysis right? To make the data world “a better place”, we need to write all of the command line in a do-file so everyone can run our data project within one click. After you test and try your commands in the command window, and if everything checks out, copy paste your commands in the do-file. The format follows:

log using YOUR_LOG_NAME.log, replace
set more off

[Your code goes here]

set more on
log close

So, after typing in the do file opening codes, you need to tell Stata:

  1. Which folder am I looking at? (using cd)
  2. Which data file contains all the variables and data? (using insheet)
  3. *If I have an dta file already saved up, which file to open? (load dta file)
  4. After you type all the commands in the command window and everything checks out, copy paste the commands to your do file and then save the do file.

Think LOG FILE as the outcome or result of the DO FILE. When you successfully run a DO FILE, your DO FILE will generate a log file with all the commands and results. MAGIC.

Stata is a dedicated and loyal machine therefore it cannot open two log files at the same time. Simply type “log close” and then you can start running your DO FILE.