1 ccpr computing services workshop: introduction to stata june, 2006
TRANSCRIPT
![Page 1: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/1.jpg)
1
CCPR Computing ServicesWorkshop: Introduction to
StataJune, 2006
![Page 2: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/2.jpg)
2
Outline Stata
Command Syntax Basic Commands Abbreviations Missing Values Combining Data Using do-files Basic programming Special Topics Getting Help Updating Stata
![Page 3: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/3.jpg)
3
Stata Syntax
Basic command syntax:[by varlist:]
command [varlist] [= exp] [if exp] [in range] [weighttype=weight] [, options]
Brackets = optional portions Italics = user specified
![Page 4: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/4.jpg)
4
Complete syntax[by varlist:]
command [varlist] [= exp] [if exp] [in range] [weighttype=weight] [, options]
Example 1 (webuse union) Stata Command:
.bysort black: summarize age if year >= 80, detail Results:
Summarizes age separately for different values of black, including only observations for which year >= 80, includes extra detail.
Stata Syntax, cont.
![Page 5: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/5.jpg)
5
Complete syntax[by varlist:]
command [varlist] [= exp] [if exp] [in range] [weighttype=weight] [, options] Example 2 (webuse union)
Stata Commands: .generate agelt30 = age.replace agelt30 = 1 if age < 30.replace agelt30 = 0 if age >= 30 & age < .
Result: Variable agelt30 set equal to 1, 0, or missing
Generally [= exp] used with commands generate and replace
Stata Syntax, cont.
![Page 6: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/6.jpg)
6
Basic Commands – Load “auto” data and look at some vars Load data from Stata’s website
webuse auto.dta Look at dataset
describe Summarize some variables
codebook make headroom, header
inspect weight length
![Page 7: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/7.jpg)
7
Basic Commands – Load “auto” data and look at some vars Look at first and last observation
list make price mpg rep78 if _n==1
list make price mpg rep78 if _n==_N Summarize a variable in a table
table foreign
table foreign, c(mean mpg sd mpg)
![Page 8: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/8.jpg)
8
Keep/Save a Subset of the Data “Keep” a subset of the variables in memory
keep make headroom trunk weight length List variables in current dataset
ds List string variables in current dataset
ds, has(type string) Save current dataset
save tempdata/myauto
![Page 9: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/9.jpg)
9
Generating New Variables
Create new variable = headroom squaredgenerate headroom2 = headroom^2
Generate numeric from string variableencode make, generate(makeNum)
list make makeNum in 1/5 Can’t tell it’s numeric, but look at “storage type” in
describe:
describe make makeNum
![Page 10: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/10.jpg)
10
Generating New Variables, cont. Create categorical variable from continuous
variable “price” is integer-valued with minimum 3291 and
max 15906 Generate categorical version - Method 1:
generate priceCat = 0
replace priceCat = 1 if price < 5000
replace priceCat = 2 if price >= 5000 & price < 10000
replace priceCat = 3 if price >= 10000 & price < .
![Page 11: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/11.jpg)
11
Generating New Variables, cont. Generate categorical version of numerical
variable: Method 2generate priceCat2 = price
recode priceCat2 (min/5000 = 1) (5000/10000=2) (10000/max=3)
Compare price, priceCat, and priceCat2table price priceCat
table priceCat priceCat2
![Page 12: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/12.jpg)
12
Variable Labels and Value Labels Create a description for a variable:
label variable priceCat “Categorical price"
Create labels to represent variable values:label define priceCatLabels 1 cheap 2 mid-range 3 expensive
label values priceCat priceCatLabels
View results:describe
list price priceCat in 1/10
![Page 13: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/13.jpg)
13
Reshape
Wide -> Long: reshape long uniqueschool author, i(year session order) j(count)
Long -> Wide:reshape wide author, i(year session order) j(count)
year Session Order Author1 Author2
2006 P01 3 Biddlecom Bankole
2006 P01 4 Anyara Hinde
2006 P01 5 Amouzou Becker
Year Session Order Author Count
2006 P01 3 Biddlecom 1
2006 P01 3 Bankole 2
2006 P01 4 Anyara 1
2006 P01 4 Hinde 2
2006 P01 5 Amouzou 1
2006 P01 5 Becker 2
Wide format:
Long format:
![Page 14: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/14.jpg)
14
A few other commands
compress - saves data more efficiently sort/ gsort order rename more
![Page 15: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/15.jpg)
15
Abbreviations in Stata
Abbreviating command, option, and variable names shortest uniquely identifying name is sufficient
Example: Assume three variables are in use: make, price, mpg “UN-abbreviated” Stata command:
.summarize make price Abbreviated Stata command:
.su ma p Exceptions
describe (d), list (l), and some others Commands that change/delete Functions implemented by ado-files
![Page 16: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/16.jpg)
16
Missing Values in Stata 8 and 9 Stata 8 and later versions
27 representations of numerical “missing” ., .a, .b, … , .z
Relational comparisons Biggest number < . < .a < .b < … < .z
Mathematical functions missing + nonmissing = missing
String missing = Empty quote: “”
![Page 17: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/17.jpg)
17
Missing Values in Stata - Pitfalls Pitfall #1
Missing values changed after Stata7:
Pitfall #2 Do NOT:
.replace weightlt200 = 0 if weight >= 200
INSTEAD: .replace weightlt200 = 0 if weight >= 200 & weight < .
Stata 7 Stata 8 and later
varname != . varname < .
varname == . varname >= .
![Page 18: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/18.jpg)
18
Combining Data
Append vs. Merge Append – two datasets with same variables, different
observations Merge – two datasets with same or related observations,
different variables
Appending data in Stata Example: append.do
![Page 19: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/19.jpg)
19
Combining Data- merge and joinby Demonstrate with two sample datasets:
Neighborhood and County samples One-to-one merge
onetoone.do One-to-many merge – use match merge
onetomany.do Many-to-many merge – use joinby
manytomany.do
![Page 20: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/20.jpg)
20
Combining Data
Variable _merge (generated by merge and joinby)
Pitfalls pitfall_merge1.do: Merging unsorted data pitfall_merge2.do : many-to-many using merge instead of
joinby
_merge Observation in master data Observation in “using” data
1 Yes No
2 No Yes
3 Yes Yes
![Page 21: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/21.jpg)
21
Do-files
What is a do-file? Stata commands can be executed interactively or
via a do-file A do-file is a text file containing commands that
can be read by Stata Running a do-file within Stata
.do dofilename.do
![Page 22: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/22.jpg)
22
Do-files
Why use a do-file? Documentation Communication Reproduce interactive session?
Interactive vs. do-files Record EVERYTHING to recreate results in
your do-file!
![Page 23: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/23.jpg)
23
Do-files > Header, Version Control Header
Include in do-files – name, project, project location, date, purpose, inputs, outputs, special instructions
Version Control include version at top of do-file Why? Example:
Under version 7, .==.a==.b==….==.z
![Page 24: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/24.jpg)
24
Do-files > Comments
Comments Lines beginning with * will be ignored Words between // and end of line will be ignored Spanning commands over two lines:
Words between /* and */ will be ignored, including end of line character
Words between /// and beginning of next line will be ignored
![Page 25: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/25.jpg)
25
Do-file > End of Line Character Commands requiring multiple lines
delimit ; This command tells Stata to read semi-colons as the
end-of-line character instead of the carriage return Comment out the carriage return with
/* at the end of line and */ at the beginning of next Comment out the carriage return with ///
![Page 26: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/26.jpg)
26
Do-files > Examples
webuse auto, clear
*this is a comment
#delimit ;summarize price mpg rep78
headroom trunk weight;#delimit cr
summarize price mpg rep78 headroom trunk weight //this is a comment
summarize price mpg rep78 /// headroom trunk weight
summarize price mpg rep78 /* */ headroom trunk weight
![Page 27: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/27.jpg)
27
Saving output
Work in do-files and log your sessions! log using filename
replace, append
log close Output choices:
*.log file - ASCII file *.smcl file - nicer format for viewing and printing in Stata
![Page 28: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/28.jpg)
28
Saving Output, cont.
Graphs are not saved in log files Use “saving” option of graph commands
saving(graph.ext) Export current graph:
graph export graph.ext Ex: graph export graph.eps
Supported formats: .ps, .eps, .wmf, .emf .pict
![Page 29: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/29.jpg)
29
Example using local macro
. local mypath "C:\Documents and Settings\MyStata"
. display `mypath'C:\Documents invalid namer(198);
. display C:\Documents and Settings\MyStataC:\Documents invalid namer(198);
. display "`mypath'"C:\Documents and Settings\MyStata
![Page 30: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/30.jpg)
30
Example– foreach, return, display*see samplePrograms.do, runLoop
foreach var of varlist tenure-lnwage {
quietly summarize `var'
local varmean = r(mean)
display "Variable `var' has mean `varmean’ "
}
![Page 31: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/31.jpg)
31
Example using forvalues, display*see samplePrograms.do, runCount
forvalues counter = 1/10 {
display `counter'
}
forvalues counter = 0(2)10 {
display `counter'
}
![Page 32: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/32.jpg)
32
Example: forvalues, generating random variables*see samplePrograms.do, runRandomGen
forvalues j = 1/3 {
generate x`j' = uniform()
generate y`j' = invnormal(uniform())
}
foreach x of varlist x1-x3 y1-y3 {
summarize `x'
}
![Page 33: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/33.jpg)
33
Example – if/else
*see samplePrograms.do, runIfElseforeach var of varlist tenure-ln_wage { quietly summarize `var' local varmean = r(mean) if `varmean' > 10 { display "`var' has mean greater than 10" } else { display "`var' has mean less than 10" }}
![Page 34: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/34.jpg)
34
Special Topic: regular expressions webuse auto List all values of make starting with a capital
and containing an additional capital:
list make if regexm(make, "^[A-Z].+[A-Z].+")
AND ending in a number
list make if regexm(make, "^[A-Z].+[A-Z].+[0-9]+$")
![Page 35: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/35.jpg)
35
Special Topic: accessing data in another database odbc list odbc query testStata odbc query testStata odbc desc "Summary2006$“ odbc load year type session order author1
author2, table("Summary2006$") dsn("testStata")
![Page 36: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/36.jpg)
36
Special Topic: Exporting results using outreg User-written program called outreg From within Stata, type findit outreg Very simple!! Basically add one line of code after each
regression to export results For an example of code, see
http://www.ats.ucla.edu/stat/stata/faq/outreg.htm
![Page 37: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/37.jpg)
37
Getting Help in Stata
help command_name abbreviated version of manual
search search keywords, local search keywords, net search keywords, all
findit keywords same as search keywords, all
Search Stata Listserver and Stata FAQ
![Page 38: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/38.jpg)
38
Stata Resources
www.stata.com > Resources and Support Search Stata Listserver Search Stata (FAQ) Stata Journal (SJ)
articles for subscribers programs free
Stata Technical Bulletin (STB) replaced with the Stata Journal Articles available for purchase, programs free
Courses (for fee)
![Page 39: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/39.jpg)
39
Updating Stata
help update update all
![Page 40: 1 CCPR Computing Services Workshop: Introduction to Stata June, 2006](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649e355503460f94b23fe7/html5/thumbnails/40.jpg)
40
Questions/feedback