The Office of Population Research at Princeton University

November 7, 2009


Administration
Faculty
Staff
Students
Jobs


Projects
Seminars
Working Papers
Publications
Dissertations


  Prospective  Students
Programs
Courses
Course Schedule


Data Archive
Library
Pop Index
NIH Public
    Access Policy


Calendar


CRCW
CHW
CMD
PUM
OPR Mail

Search

 

Basic Stata

This is a basic introduction to Stata. In this module we will cover Stata's user interface, getting help, installing "STB" programs and updating Stata, how to "use" data, and "do" and "log" files.

Stata's user interface

When you first open Stata, the window is rather small. You can, of course make the windows larger, but the type remains small. You can change the fonts as well by clicking on the window icon in the top left corner of any of the Stata windows.

Stata, by default opens with four windows:

  • Results - where all of your commands and their results are displayed (with the exception of graphs which are displayed in their own window). Anything displayed in blue can be clicked on to get help or other information.
  • Review - just your commands are displayed here. You can click on any command in the window and it will be pasted to the command window. The Review window has one extra option in its windows icon menu: "Save Review Contents." This will allow you to save everything in the review window to a file for later use.  This is not a substitute for using log and do files!!!
  • Command - this is where you type your commands when working in interactive mode. Everything you type in here is echoed in the Results window as well as the Review window. The "Page Up" and "Page Down" keys can be used to scroll back and forth through commands you have executed previously. You can also copy and paste between this window and your "do" file.
  • Variables - a list of all your variables and their labels is displayed here. You can click on a variable here and it will be pasted to the command window.

There are three other windows in Stata:

  • Do-file editor - this is Stata's simple text editor for writing do-files, or programs. You should do all of your work in a do-file so you can reproduce what you did later on.
  • Viewer - the viewer is used for displaying help and log files. Like the Results window, anything displayed in blue can be clicked on for more information.
  • Graph - as you may have guessed, this is where all of your graphs will be displayed.

Getting Help in Stata

There are several ways of getting help in Stata. The first, of course, is the "help" command. You can enter the help command from the Command window and the help text will be displayed in the Results window. Or, you can use the "Stata command" option in the Help menu and the help text will be displayed in the Viewer window.

The "help" command is good only if you know the command for which you want help. If you do not know the command, then you should use the "search" command which actually does a keyword search of Stata documentation. The search command (or help menu option) has two versions: one which limits the search to your computer and one which searches Stata's website. Of course, you must be connected to the Internet to search the website ("Search net resources"), and if you are, this is the better choice.

Often when you use the search or help in Stata you will find commands that do exactly what you want, but when you try to use them on your own computer, you get an "unrecognized command" error. That is because many commands in Stata have been written by other Stata users and are not part of "official" Stata. These are often referred to as the "STB" (Stata Technical Bulletin) or "User-written" programs and you must install these commands yourself. Stata makes this very easy as you can just click your way through various menus in the viewer.

Every two months Stata releases an update. This update adds functionality to Stata as well as fixing bugs that may have been found since the previous update was released. You can update Stata by simply selecting the "Official Updates" option under the "Help" menu and following the instructions.

Using Data

Stata wouldn't be of much use unless you had some data to analyze. There are several ways of getting data into Stata, but the easiest is the "use" command. The "use" command is only for data that are already in Stata format (as opposed to text, "raw" or Excel). To get data into Stata, you simply need to give the command "use mydata" where we assume the name of your data file is "mydata.dta". You can also "use" data that are stored somewhere on the Internet by simply using the URL and the filename.

When you are done, you must save your data and you do this with the "save" command. If you have made many changes to your data, then you will either need to specify a new name for the file or include the "replace" option. Stata is pretty good about not letting you shoot yourself in the foot, but it is not foolproof.

Do, Log and Cmdlog files

There are two cardinal rules when working with Stata:

  • Always do your work in a do-file!
  • Always have a log file running!

Having a do-file is the only way to make sure that you can reproduce your results at a later time! They are a record of what you did and why (assuming you added plenty of comments). Log files also are a record of what you did, but they also contain the results of what you did. It wouldn't make much sense to spend several hours analyzing data and not keep the results, now would it? A command log, or "cmdlog" is a record of ONLY the commands you execute and is especially important if you use the pull-down menus to do your work.

You can start a do-file by simply clicking on the do-file editor button. Make sure that the "Auto indent" and "Auto save on do/run" options are checked in the Edit>Preferences menu. You can enter any valid Stata command in the editor and click on "Run" to run all of the commands. You can even highlight one or more commands to run only those commands.

You can start a log file with the command "log using mylog.log". When you use ".log" as the extension, Stata automatically creates the log as a plain text file that can then be opened in MSWord or notepad as well as Stata’s viewer. Everything you see in the Results window goes into the log with the exception of output from the "help" command and graphs created with the "graph" command. When you are done, you can close the log with the "log close" command.

You can start a command log with the command "cmdlog using mycmdlog.log". Like regular logs, using the ".log" extension makes sure the file is in text format. As mentioned above, this file has only the commands you execute, no output. Additionally, all commands, whether they are issued from the command window, pull-down menus, or a do-file are recorded in the command log. You close the command log with the command "cmdlog close".

On to the next tutorial, Reading and Documenting Data

top
Mail: Office of Population Research, Princeton University, Wallace Hall, Princeton NJ 08544
Phone: (609) 258-4870  •  Fax: (609) 258-1039  •  Email: webmaster@opr.princeton.edu