Introduction Stata Slides 2
Introduction Stata Slides 2
Stata
1
About
STATA is modern and general command driven package for statistical
analyses, data management and graphics.
2
Starting and stopping Stata
Use shortcut or taskbar icon.
3
Opening Stata data files
Stata has its own format for data files
extension *.dta
Choose FileOpen
Go to S:\...\auto.dta
Importing other file formats is possible
4
Importing data
Stata can also read tab-delimited ASCII text
files
Most other software (e.g., Excel)
can write tab-delimited ASCII text files
Let’s get data from Excel…
5
Saving data
“Preserve” saves only a temporary copy of the data
file.
The original data file is unaffected.
To save a permanent data file,
Choose FileSave As…
Navigate to your X: drive
X: is where you should save things
Save as “my_example1.dta”
8
4 windows
Stata gives you 4 windows: Command, Results, Review, and
Variables. Command: type a command here and press
Enter
Results: the results of your command are displayed here
Review: each command you type is displayed here
Click on a command to put it into the command window for editing
Double-click on a command to execute it directly
Variables: lists the variables in memory
Click on a variable name to put it into the command window
You can resize these 4 windows independently, and you can
resize the outer window as well. To save your window size
changes, click on Edit, Preferences, Save Preferences Set
9
Review window: Results window:
Past commands. Output and past commands
Click to paste in Command window
Variables window:
List of variables in open data set. Command window:
Click to paste in Command window. Current command
6
Examining data
Move
selected Hide
column Delete
selected
Undo changes to the end selected
column Close
Sort by columns
since last “save”
selected or rows editor
“Save” column
Change
selected
value
7
Menu Bar
Stata displays 8 drop-down menus across the top of the outer window: File
Open: open a Stata data file (use)
Save/Save as: save the Stata data in memory to disk
Do: execute a do-file
Filename: copy a filename to the command line
Print: print log or graph
Exit: quit Stata
Edit
Copy/Paste: copy text among the Command, Results, and Log windows
Copy Table: copy table from Results window to another file
Table copy options: what to do with table lines in Copy Table
Data, Graphics, Statistics - build and run Stata commands from menus
User - menus for user-supplied Stata commands (download from Internet)
Window - bring a Stata window to the front
Help - The Stata manual set in PDF format plus Stata command syntax and
keyword searches
10
Tool bar
The buttons on the button bar are from left to right (equivalent
command is in bold): Open a Stata data file: use
Save the Stata data in memory to disk: save
Print a log or graph
Open a log, or suspend/close an open log: log
Open a new viewer window (to view Help or a log file)
Bring the graph window to the front (if you've created a graph)
Open a do-file
Edit the data in memory: edit
Browse the data in memory: browse
Open the Variables Manager
Scroll another page when --more-- is displayed: Space Bar
Stop current command or do-file: Ctrl-Break
11
Sources of help
12
Sources of help
Help menu
Command: almost the full reference manual for each Stata
command
Search: keyword search of the manuals, technical bulletins, and
frequently asked questions
and lots more!
13
Basic Operations
Entering Data
Exploring Data
Modifying Data
Managing Data
14
Entering Data
Insheet: Read ASCII (text) data created by a spreadsheet (.csv files only)
Input: Enter data from keyboard
Describe: Describe contents of data in memory or on disk
Save: Store the dataset currently in memory on disk in Stata data format
Count: Show the number of observations
List: List values of variables
Clear: Clear the entire dataset and everything else
15
Exploring data
Describe: Describe a dataset
List List the contents of a dataset
Codebook: Detailed contents of a dataset
Log: Create a log file
Summarize: Descriptive statistics
Tabstat: Table of descriptive statistics
Table: Create a table of statistics
Stem: Stem-and-leaf plot
Graph: High resolution graphs
Kdensity: Kernal density plot
Sort: Sort observations in a dataset
Histogram: Histogram for continuous and categorical variables
Tabulate: One- and two-way frequency tables
Correlate: Correlations
Pwcorr: Pairwise correlations
Type: Display an ASCII file
16
Modifying Data
label data: Apply a label to a data set
Order: Order the variables in a data set
label variable: Apply a label to a variable
label define: Define a set of a labels for the levels of a categorical variable
label values: Apply value labels to a variable
List: Lists the observations
Rename: Rename a variable
Recode: Recode the values of a variable
Notes: Apply notes to the data file
Generate: Creates a new variable
Replace: Replaces one value with another value
Egen: Extended generate - has special functions that can be used when creating a new
variable
17
Labeling variables
To add a descriptive label to a variable
DataLabelsLabel variable
Add these labels to these variables:
bwt : “Birth weight, in grams”
smoke : “Did mother smoke during pregnancy?”
18
Labeling values
Many variables are dummy variables
two values: 0 and 1
e.g., “Did the mother smoke?” Yes (1) or no (0).
To add labels to dummy values
DataLabelsLabel ValuesDefine or Modify Value
Labels
Define label name: “dummy”
Add values
1 means “yes”
0 means “no”
Now tell Stata that smoke is a dummy variable
DataLabelsLabel ValuesAssign value label to variable
Look at smoke in the Data Editor
and double-click it
19
Generating and Recoding Variables
gen quality=0
recode quality 0=1 if VA==1 or
replace quality=1 if VA==1
gen
20
Creating a new variable
Let’s create a dummy variable
Data
Create or change variable
Create a new variable
21
Managing Data
Pwd: Show current directory (pwd=print working directory)
dir or ls: Show files in current directory
cd Change directory
keep if: Keep observations if condition is met
Keep: Keep variables (dropping others)
Drop: Drop variables (keeping others)
append using: Append a data file to current file
Merge: Merge a data file with current file
22
Syntax: Commands
Command Recommended Usage
Describe d Describe data in memory
generate gen Create new variables
graph graph Graph data
help h Call online help
list l List data
regress reg Linear regression
summarize sum Descriptive statistics
save save Save data in memory
sort sort Sort data
tabulate tab Tables of frequencies
use use Load data into memory
23
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
To open a do file editor: Window – Do-file Editor or Ctrl + 8
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
24
Do file – cont.
Look at the commands in a do-file that contains:
. type hsbbatch.do
To run the do-file.
do hsbbatch
From do file, choose Tools - Do
25