SlideShare a Scribd company logo
1 of 1
Download to read offline
Programming
Cheat Sheetwith Stata 14.1
For more info see Stata’s reference manual (stata.com)
Tim Essam (tessam@usaid.gov) • Laura Hughes (lhughes@usaid.gov)
follow us @StataRGIS and @flaneuseks
inspired by RStudio’s awesome Cheat Sheets (rstudio.com/resources/cheatsheets) updated June 2016
CC BY 4.0
geocenter.github.io/StataTraining
Disclaimer: we are not affiliated with Stata. But we like it.
PUTTING IT ALL TOGETHER sysuse auto, clear
generate car_make = word(make, 1)
levelsof car_make, local(cmake)
local i = 1
local cmake_len : word count `cmake'
foreach x of local cmake {
display in yellow "Make group `i' is `x'"
if `i' == `cmake_len' {
display "The total number of groups is `i'"
}
local i = `++i'
}
define the
local i to be
an iterator
tests the position of the
iterator, executes contents
in brackets when the
condition is true
increment iterator by one
store the length of local
cmake in local cmake_len
calculate unique groups of
car_make and store in local cmake
pull out the first word
from the make variable
see also capture and scalar _rc
Stata has three options for repeating commands over lists or values:
foreach, forvalues, and while. Though each has a different first line,
the syntax is consistent:
Loops: Automate Repetitive Tasks
ANATOMY OF A LOOP see also while
i = 10(10)50 10, 20, 30, ...
i = 10 20 to 50 10, 20, 30, ...
i = 10/50 10, 11, 12, ...
ITERATORS
DEBUGGING CODE
set trace on (off)
trace the execution of programs for error checking
foreach x of varlist var1 var2 var3 {
command `x', option
}
open brace must
appear on first line
temporary variable used
only within the loop
objects to repeat over
close brace must appear
on final line by itself
command(s) you want to repeat
can be one line or many
...
requires local macro notation
FOREACH: REPEAT COMMANDS OVER STRINGS, LISTS, OR VARIABLES
FORVALUES: REPEAT COMMANDS OVER LISTS OF NUMBERS
display 10
display 20
...
display length("Dr. Nick")
display length("Dr. Hibbert")
When calling a command that takes a string,
surround the macro name with quotes.
foreach x in "Dr. Nick" "Dr. Hibbert" {
display length ( "` x '" )
}
LISTS
sysuse "auto.dta", clear
tab rep78, missing
sysuse "auto2.dta", clear
tab rep78, missing
same as...
loops repeat the same command
over different arguments:
foreach x in auto.dta auto2.dta {
sysuse "`x'", clear
tab rep78, missing
}
STRINGS
summarize mpg
summarize weight
• foreach in takes any list
as an argument with
elements separated by
spaces
• foreach of requires you
to state the list type,
which makes it faster
foreach x in mpg weight {
summarize `x'
}
foreach x of varlist mpg weight {
summarize `x'
}
must define list type
VARIABLES
Use display command to
show the iterator value at
each step in the loop
foreach x in|of [ local, global, varlist, newlist, numlist ] {
Stata commands referring to `x'
}
list types: objects over which the
commands will be repeated
forvalues i = 10(10)50 {
display `i'
}
numeric values over
which loop will run
iterator
Additional Programming Resources
install a package from a Github repository
net install package, from (https://raw.githubusercontent.com/username/repo/master)
download all examples from this cheat sheet in a .do file
bit.ly/statacode
https://github.com/andrewheiss/SublimeStataEnhanced
configure Sublime text for Stata 11-14
adolist
List/copy user-written .ado files
adoupdate
Update user-written .ado files
ssc install adolist
The estout and outreg2 packages provide numerous, flexible options for making tables
after estimation commands. See also putexcel command.
EXPORTING RESULTS
esttab using “auto_reg.txt”, replace plain se
export summary table to a text file, include standard errors
outreg2 [est1 est2] using “auto_reg2.txt”, see replace
export summary table to a text file using outreg2 syntax
esttab est1 est2, se star(* 0.10 ** 0.05 *** 0.01) label
create summary table with standard errors and labels
Access & Save Stored r- and e-class Objects4
mean price
returns list of scalars, macros,
matrices and functions
summarize price, detail
returns a list of scalars
return list ereturn lister
Many Stata commands store results in types of lists. To access these, use return or
ereturn commands. Stored results can be scalars, macros, matrices or functions.
create a temporary copy of active dataframepreserve
restore temporary copy to original pointrestore
create a new variable equal to
average of price
generate p_mean = r(mean)
scalars:
e(df_r) = 73
e(N_over) = 1
e(k_eq) = 1
e(rank) = 1
e(N) = 73
scalars:
...
r(N) = 74
r(sd) = 2949.49...
r(mean) = 6165.25...
r(Var) = 86995225.97...
create a new variable equal to
obs. in estimation command
generate meanN = e(N)
Results are replaced
each time an r-class
/ e-class command
is called
set restore points to test
code that changes data
create local variable called myLocal with the
strings price mpg and length
local myLocal price mpg length
levelsof rep78, local(levels)
create a sorted list of distinct values of rep78,
store results in a local macro called levels
summarize ` myLocal '
summarize contents of local myLocal
add a ` before and a ' after local macro name to call
PRIVATEavailable only in programs, loops, or .do filesLOCALS
local varLab: variable label foreign
store the variable label for foreign in the local varLab
can also do with value labels
tempfile myAuto
save `myAuto'
create a temporary file to
be used within a program
tempvar temp1
generate `temp1' = mpg^2
summarize `temp1'
initialize a new temporary variable called temp1
summarize the temporary variable temp1
save squared mpg values in temp1
special locals for loops/programsTEMPVARS & TEMPFILES
Macros3 public or private variables storing text
global pathdata "C:/Users/SantasLittleHelper/Stata"
define a global variable called pathdata
available through Stata sessions PUBLICGLOBALS
global myGlobal price mpg length
summarize $myGlobal
summarize price mpg length using global
cd $pathdata
change working directory by calling global macro
add a $ before calling a global macro
see also
tempname
matselrc b x, c(1 3)
select columns 1 & 3 of matrix b & store in new matrix x
findit matselrc
mat2txt, matrix(ad1) saving(textfile.txt) replace
ssc install mat2txtexport a matrix to a text file
Matrices2 e-class results are stored as matrices
matrix ad1 = a  d
row bind matrices
matrix ad2 = a , d
column bind matrices
matrix a = (4 5 6)
create a 3 x 1 matrix
matrix b = (7, 8, 9)
create a 1 x 3 matrix
matrix d = b' transpose matrix b; store in d
scalar a1 = “I am a string scalar”
create a scalar a1 storing a string
Scalars1 both r- and e-class results contain scalars
scalar x1 = 3
create a scalar x1 storing the number 3
Scalars can hold
numeric values or
arbitrarily long strings
DISPLAYING & DELETING BUILDING BLOCKS
[scalar | matrix | macro | estimates] [list | drop] b
list contents of object b or drop (delete) object b
[scalar | matrix | macro | estimates] dir
list all defined objects for that class
list contents of matrix b
matrix list b
list all matrices
matrix dir
delete scalar x1
scalar drop x1
Use estimates store
to compile results
for later use
estimates table est1 est2 est3
print a table of the two estimation results est1 and est2
estimates store est1
store previous estimation results est1 in memory
regress price weight
eststo est2: regress price weight mpg
eststo est3: regress price weight mpg foreign
estimate two regression models and store estimation results
ssc install estout
ACCESSING ESTIMATION RESULTS
After you run any estimation command, the results of the estimates are
stored in a structure that you can save, view, compare, and export
basic components of programming
2 rectangular array of quantities or expressions
3 pointers that store text (global or local)
1
MATRICES
MACROS
SCALARS individual numbers or strings
R- AND E-CLASS:Stata stores calculation results in two*
main classes:
r ereturn results from general commands
such as summary or tabulate
return results from estimation
commands such as regress or mean
To assign values to individual variables use:
e
e
r
Building Blocks
* there’s also s- and n-class

More Related Content

What's hot

Introduction to probability
Introduction to probabilityIntroduction to probability
Introduction to probabilityDavid Radcliffe
 
Simulacra and simulation
Simulacra and simulationSimulacra and simulation
Simulacra and simulationMZurek
 
Preference Elicitation in Recommender Systems
Preference Elicitation in Recommender SystemsPreference Elicitation in Recommender Systems
Preference Elicitation in Recommender SystemsAnish Shenoy
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2izahn
 
Probability Concepts Applications
Probability Concepts  ApplicationsProbability Concepts  Applications
Probability Concepts Applicationsguest44b78
 
Postmodernism theories and texts
Postmodernism theories and textsPostmodernism theories and texts
Postmodernism theories and textsraybloggs
 
Machine Learning for retail and ecommerce
Machine Learning for retail and ecommerceMachine Learning for retail and ecommerce
Machine Learning for retail and ecommerceAndrei Lopatenko
 
Jean Baudrillard’s Theory
Jean Baudrillard’s TheoryJean Baudrillard’s Theory
Jean Baudrillard’s TheoryZoe Lorenz
 

What's hot (10)

Introduction to probability
Introduction to probabilityIntroduction to probability
Introduction to probability
 
Simulacra and simulation
Simulacra and simulationSimulacra and simulation
Simulacra and simulation
 
Preference Elicitation in Recommender Systems
Preference Elicitation in Recommender SystemsPreference Elicitation in Recommender Systems
Preference Elicitation in Recommender Systems
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
 
Probability Concepts Applications
Probability Concepts  ApplicationsProbability Concepts  Applications
Probability Concepts Applications
 
Postmodernism theories and texts
Postmodernism theories and textsPostmodernism theories and texts
Postmodernism theories and texts
 
Machine Learning for retail and ecommerce
Machine Learning for retail and ecommerceMachine Learning for retail and ecommerce
Machine Learning for retail and ecommerce
 
Python - the basics
Python - the basicsPython - the basics
Python - the basics
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Jean Baudrillard’s Theory
Jean Baudrillard’s TheoryJean Baudrillard’s Theory
Jean Baudrillard’s Theory
 

Viewers also liked

Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Laura Hughes
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processingTim Essam
 
Stata cheat sheet: data visualization
Stata cheat sheet: data visualizationStata cheat sheet: data visualization
Stata cheat sheet: data visualizationTim Essam
 
Stata cheat sheet: Data visualization
Stata cheat sheet: Data visualizationStata cheat sheet: Data visualization
Stata cheat sheet: Data visualizationLaura Hughes
 
Stata cheatsheet transformation
Stata cheatsheet transformationStata cheatsheet transformation
Stata cheatsheet transformationLaura Hughes
 
STATA - Probit Analysis
STATA - Probit AnalysisSTATA - Probit Analysis
STATA - Probit Analysisstata_org_uk
 

Viewers also liked (6)

Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processing
 
Stata cheat sheet: data visualization
Stata cheat sheet: data visualizationStata cheat sheet: data visualization
Stata cheat sheet: data visualization
 
Stata cheat sheet: Data visualization
Stata cheat sheet: Data visualizationStata cheat sheet: Data visualization
Stata cheat sheet: Data visualization
 
Stata cheatsheet transformation
Stata cheatsheet transformationStata cheatsheet transformation
Stata cheatsheet transformation
 
STATA - Probit Analysis
STATA - Probit AnalysisSTATA - Probit Analysis
STATA - Probit Analysis
 

Similar to Stata Programming Cheat Sheet

Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programmingTim Essam
 
Cheat Sheet for Stata v15.00 PDF Complete
Cheat Sheet for Stata v15.00 PDF CompleteCheat Sheet for Stata v15.00 PDF Complete
Cheat Sheet for Stata v15.00 PDF CompleteTsamaraLuthfia1
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab FunctionsUmer Azeem
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply FunctionSakthi Dasans
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Barry DeCicco
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.jstimourian
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»e-Legion
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene BurmakoVasil Remeniuk
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
CSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxCSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxfaithxdunce63732
 

Similar to Stata Programming Cheat Sheet (20)

Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programming
 
Matlab Manual
Matlab ManualMatlab Manual
Matlab Manual
 
Cheat Sheet for Stata v15.00 PDF Complete
Cheat Sheet for Stata v15.00 PDF CompleteCheat Sheet for Stata v15.00 PDF Complete
Cheat Sheet for Stata v15.00 PDF Complete
 
INTRODUCTION TO STATA.pptx
INTRODUCTION TO STATA.pptxINTRODUCTION TO STATA.pptx
INTRODUCTION TO STATA.pptx
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab Functions
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function
 
Mysql1
Mysql1Mysql1
Mysql1
 
Practical cats
Practical catsPractical cats
Practical cats
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Aggregate.pptx
Aggregate.pptxAggregate.pptx
Aggregate.pptx
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
CSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docxCSC8503 Principles of Programming Languages Semester 1, 2015.docx
CSC8503 Principles of Programming Languages Semester 1, 2015.docx
 

Recently uploaded

Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...Bertram Ludäscher
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareGraham Ware
 
sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444saurabvyas476
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRajesh Mondal
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchersdarmandersingh4580
 
jll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdfjll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdfjaytendertech
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...ThinkInnovation
 
👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...
👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...
👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...vershagrag
 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?RemarkSemacio
 
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...Klinik Aborsi
 
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarjSCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarjadimosmejiaslendon
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxronsairoathenadugay
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNKTimothy Spann
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxParas Gupta
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格q6pzkpark
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证acoha1
 
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...mikehavy0
 
社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token PredictionNABLAS株式会社
 

Recently uploaded (20)

Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchers
 
jll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdfjll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdf
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
 
👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...
👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...
👉 Tirunelveli Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Gir...
 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?
 
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
Jual Obat Aborsi Bandung (Asli No.1) Wa 082134680322 Klinik Obat Penggugur Ka...
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarjSCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptx
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
 
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get CytotecAbortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
 
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
 
社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction
 

Stata Programming Cheat Sheet

  • 1. Programming Cheat Sheetwith Stata 14.1 For more info see Stata’s reference manual (stata.com) Tim Essam (tessam@usaid.gov) • Laura Hughes (lhughes@usaid.gov) follow us @StataRGIS and @flaneuseks inspired by RStudio’s awesome Cheat Sheets (rstudio.com/resources/cheatsheets) updated June 2016 CC BY 4.0 geocenter.github.io/StataTraining Disclaimer: we are not affiliated with Stata. But we like it. PUTTING IT ALL TOGETHER sysuse auto, clear generate car_make = word(make, 1) levelsof car_make, local(cmake) local i = 1 local cmake_len : word count `cmake' foreach x of local cmake { display in yellow "Make group `i' is `x'" if `i' == `cmake_len' { display "The total number of groups is `i'" } local i = `++i' } define the local i to be an iterator tests the position of the iterator, executes contents in brackets when the condition is true increment iterator by one store the length of local cmake in local cmake_len calculate unique groups of car_make and store in local cmake pull out the first word from the make variable see also capture and scalar _rc Stata has three options for repeating commands over lists or values: foreach, forvalues, and while. Though each has a different first line, the syntax is consistent: Loops: Automate Repetitive Tasks ANATOMY OF A LOOP see also while i = 10(10)50 10, 20, 30, ... i = 10 20 to 50 10, 20, 30, ... i = 10/50 10, 11, 12, ... ITERATORS DEBUGGING CODE set trace on (off) trace the execution of programs for error checking foreach x of varlist var1 var2 var3 { command `x', option } open brace must appear on first line temporary variable used only within the loop objects to repeat over close brace must appear on final line by itself command(s) you want to repeat can be one line or many ... requires local macro notation FOREACH: REPEAT COMMANDS OVER STRINGS, LISTS, OR VARIABLES FORVALUES: REPEAT COMMANDS OVER LISTS OF NUMBERS display 10 display 20 ... display length("Dr. Nick") display length("Dr. Hibbert") When calling a command that takes a string, surround the macro name with quotes. foreach x in "Dr. Nick" "Dr. Hibbert" { display length ( "` x '" ) } LISTS sysuse "auto.dta", clear tab rep78, missing sysuse "auto2.dta", clear tab rep78, missing same as... loops repeat the same command over different arguments: foreach x in auto.dta auto2.dta { sysuse "`x'", clear tab rep78, missing } STRINGS summarize mpg summarize weight • foreach in takes any list as an argument with elements separated by spaces • foreach of requires you to state the list type, which makes it faster foreach x in mpg weight { summarize `x' } foreach x of varlist mpg weight { summarize `x' } must define list type VARIABLES Use display command to show the iterator value at each step in the loop foreach x in|of [ local, global, varlist, newlist, numlist ] { Stata commands referring to `x' } list types: objects over which the commands will be repeated forvalues i = 10(10)50 { display `i' } numeric values over which loop will run iterator Additional Programming Resources install a package from a Github repository net install package, from (https://raw.githubusercontent.com/username/repo/master) download all examples from this cheat sheet in a .do file bit.ly/statacode https://github.com/andrewheiss/SublimeStataEnhanced configure Sublime text for Stata 11-14 adolist List/copy user-written .ado files adoupdate Update user-written .ado files ssc install adolist The estout and outreg2 packages provide numerous, flexible options for making tables after estimation commands. See also putexcel command. EXPORTING RESULTS esttab using “auto_reg.txt”, replace plain se export summary table to a text file, include standard errors outreg2 [est1 est2] using “auto_reg2.txt”, see replace export summary table to a text file using outreg2 syntax esttab est1 est2, se star(* 0.10 ** 0.05 *** 0.01) label create summary table with standard errors and labels Access & Save Stored r- and e-class Objects4 mean price returns list of scalars, macros, matrices and functions summarize price, detail returns a list of scalars return list ereturn lister Many Stata commands store results in types of lists. To access these, use return or ereturn commands. Stored results can be scalars, macros, matrices or functions. create a temporary copy of active dataframepreserve restore temporary copy to original pointrestore create a new variable equal to average of price generate p_mean = r(mean) scalars: e(df_r) = 73 e(N_over) = 1 e(k_eq) = 1 e(rank) = 1 e(N) = 73 scalars: ... r(N) = 74 r(sd) = 2949.49... r(mean) = 6165.25... r(Var) = 86995225.97... create a new variable equal to obs. in estimation command generate meanN = e(N) Results are replaced each time an r-class / e-class command is called set restore points to test code that changes data create local variable called myLocal with the strings price mpg and length local myLocal price mpg length levelsof rep78, local(levels) create a sorted list of distinct values of rep78, store results in a local macro called levels summarize ` myLocal ' summarize contents of local myLocal add a ` before and a ' after local macro name to call PRIVATEavailable only in programs, loops, or .do filesLOCALS local varLab: variable label foreign store the variable label for foreign in the local varLab can also do with value labels tempfile myAuto save `myAuto' create a temporary file to be used within a program tempvar temp1 generate `temp1' = mpg^2 summarize `temp1' initialize a new temporary variable called temp1 summarize the temporary variable temp1 save squared mpg values in temp1 special locals for loops/programsTEMPVARS & TEMPFILES Macros3 public or private variables storing text global pathdata "C:/Users/SantasLittleHelper/Stata" define a global variable called pathdata available through Stata sessions PUBLICGLOBALS global myGlobal price mpg length summarize $myGlobal summarize price mpg length using global cd $pathdata change working directory by calling global macro add a $ before calling a global macro see also tempname matselrc b x, c(1 3) select columns 1 & 3 of matrix b & store in new matrix x findit matselrc mat2txt, matrix(ad1) saving(textfile.txt) replace ssc install mat2txtexport a matrix to a text file Matrices2 e-class results are stored as matrices matrix ad1 = a d row bind matrices matrix ad2 = a , d column bind matrices matrix a = (4 5 6) create a 3 x 1 matrix matrix b = (7, 8, 9) create a 1 x 3 matrix matrix d = b' transpose matrix b; store in d scalar a1 = “I am a string scalar” create a scalar a1 storing a string Scalars1 both r- and e-class results contain scalars scalar x1 = 3 create a scalar x1 storing the number 3 Scalars can hold numeric values or arbitrarily long strings DISPLAYING & DELETING BUILDING BLOCKS [scalar | matrix | macro | estimates] [list | drop] b list contents of object b or drop (delete) object b [scalar | matrix | macro | estimates] dir list all defined objects for that class list contents of matrix b matrix list b list all matrices matrix dir delete scalar x1 scalar drop x1 Use estimates store to compile results for later use estimates table est1 est2 est3 print a table of the two estimation results est1 and est2 estimates store est1 store previous estimation results est1 in memory regress price weight eststo est2: regress price weight mpg eststo est3: regress price weight mpg foreign estimate two regression models and store estimation results ssc install estout ACCESSING ESTIMATION RESULTS After you run any estimation command, the results of the estimates are stored in a structure that you can save, view, compare, and export basic components of programming 2 rectangular array of quantities or expressions 3 pointers that store text (global or local) 1 MATRICES MACROS SCALARS individual numbers or strings R- AND E-CLASS:Stata stores calculation results in two* main classes: r ereturn results from general commands such as summary or tabulate return results from estimation commands such as regress or mean To assign values to individual variables use: e e r Building Blocks * there’s also s- and n-class