poli 5d social science data analytics · poli 5d social science data analytics functions in excel...
TRANSCRIPT
Poli 5D Social Science Data AnalyticsFunctions in Excel (2); Intro to Stata
Shane Xinyang XuanShaneXuan.com
January 25, 2017
ShaneXuan.com 1 / 14
Contact Information
Shane Xinyang Xuan
The teaching sta↵ is a team!
Professor Roberts M 1600-1800 (SSB 299)Jason Bigenho Th 1000-1200 (Econ 116)Shane Xuan Th 1200-1400 (SSB 332)
Supplemental Materials
UCLA STATA starter kithttp://www.ats.ucla.edu/stat/stata/sk/
Princeton data analysishttp://dss.princeton.edu/training/
ShaneXuan.com 2 / 14
Road map
Here is what we are going to cover in today’s section:
– Logical functions in Excel
– Nested IF functions in Excel
– Macros in Excel
– Introduction to Stata
Let’s take a poll really quick – in which order do you want me tocover these topics in today’s section?
– Should I slow down in the section?
– Any questions that I can answer (w.r.t lectures, sections,homework, ...)?
ShaneXuan.com 3 / 14
Road map
Here is what we are going to cover in today’s section:
– Logical functions in Excel
– Nested IF functions in Excel
– Macros in Excel
– Introduction to Stata
Let’s take a poll really quick – in which order do you want me tocover these topics in today’s section?
– Should I slow down in the section?
– Any questions that I can answer (w.r.t lectures, sections,homework, ...)?
ShaneXuan.com 3 / 14
Road map
Here is what we are going to cover in today’s section:
– Logical functions in Excel
– Nested IF functions in Excel
– Macros in Excel
– Introduction to Stata
Let’s take a poll really quick – in which order do you want me tocover these topics in today’s section?
– Should I slow down in the section?
– Any questions that I can answer (w.r.t lectures, sections,homework, ...)?
ShaneXuan.com 3 / 14
Road map
Here is what we are going to cover in today’s section:
– Logical functions in Excel
– Nested IF functions in Excel
– Macros in Excel
– Introduction to Stata
Let’s take a poll really quick – in which order do you want me tocover these topics in today’s section?
– Should I slow down in the section?
– Any questions that I can answer (w.r.t lectures, sections,homework, ...)?
ShaneXuan.com 3 / 14
Logical functions
We are going to go through four examples in the next few slides towrap up logical functions.
ShaneXuan.com 4 / 14
Logical functions
ShaneXuan.com 4 / 14
Logical functions
ShaneXuan.com 4 / 14
Logical functions
ShaneXuan.com 4 / 14
Logical functions
ShaneXuan.com 4 / 14
Nested IF
Scenario: You want to test for multiple outcomes.Solution: A nested IF function is when one IF function is placedinside another IF function to test an additional condition.
ShaneXuan.com 5 / 14
Macros in Excel
Scenario: We want to perform repetitive tasks consistentlySolution: Use macros!
I You record a macro
I You then apply the macro to other cells
ShaneXuan.com 6 / 14
Macros in Excel
Record Macro
ShaneXuan.com 6 / 14
Macros in Excel
Stop Recording
ShaneXuan.com 6 / 14
Macros in Excel
In a new worksheet, apply Macros
ShaneXuan.com 6 / 14
Macros in Excel
What’s under the hood?
ShaneXuan.com 6 / 14
We are done with Excel!
We are done with Excel! What to do next?
I Check out VBA if you want know more about Excel
I We are going to move on to Stata and R for the rest of thequarter
Before then, let’s take attendance.
ShaneXuan.com 7 / 14
Introduction to Stata
I Change CD (cd “/Users/Shane/Dropbox/Poli5D/psets/”)
I Import data
I Summarize variable
I IF statement in Stata
I Lookup function in Stata
I Help command
I Sort command
I Create .log files
ShaneXuan.com 8 / 14
Import data in Stata
ShaneXuan.com 9 / 14
Import data in Stata
Example:import excel “h1 fams data.xlsx”, sheet(“Families”) firstrow clear
ShaneXuan.com 9 / 14
Import data in Stata
Example:import excel “h1 fams data.xlsx”, sheet(“Families”) firstrow clear
I import data
ShaneXuan.com 9 / 14
Import data in Stata
Example:import excel “h1 fams data.xlsx”, sheet(“Families”) firstrow clear
I specify sheet name
ShaneXuan.com 9 / 14
Import data in Stata
Example:import excel “h1 fams data.xlsx”, sheet(“Families”) firstrow clear
I Make the first row to be variable names
ShaneXuan.com 9 / 14
Import data in Stata
Your data look like:
ShaneXuan.com 9 / 14
Summarize variables in Stata
Example:
ShaneXuan.com 10 / 14
Summarize variables in Stata
Example:
ShaneXuan.com 10 / 14
Summarize variables in Stata
Example:
ShaneXuan.com 10 / 14
IF statements in Stata
Example:
Syntax:
I Count if (variable == value)
I Other operations: ! = > < <= >=
Note: In Stata, the single equal (=) is used as a set equaloperator; the double equals (==) is used to test for equality;exclamation mark (!) means “NOT”
ShaneXuan.com 11 / 14
IF statements in Stata
Example:
Note: In Stata, & means “AND”, and | means “OR”
ShaneXuan.com 11 / 14
IF statements in Stata
Example:
Note: In Stata, the single equal (=) is used as a set equaloperator; the double equals (==) is used to test for equality
ShaneXuan.com 11 / 14
Others
I Lookup function: list if (variable) == (value)
I Help command: help tabulate
I Sort command: sort (variable)I .log file:
– log using “log example”, text– clear more
... (your code here)– log close
ShaneXuan.com 12 / 14
Save your .do file!
ShaneXuan.com 13 / 14