1 CCPR Computing Services Introduction to Stata Courtney Engel October 26, 2007.

Slides:



Advertisements
Similar presentations
Stata as a Data Entry Management Tool
Advertisements

Do files, log files, and workflow in Stata Biostatistics 212 Lecture 2.
Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
Stata and logit recap. Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
RESEARCH WORKFLOW USING STATA How to Be an Effective Researcher CCPR Workshop.
1. Overview Brief guide to the display windows and toolbar
CCPR Computing Services More Efficient Programming July 13, 2006.
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
INTRODUCTION TO STATA Võ Tuấn Khoa Trần Thế Trung.
Today: Run SAS programs on Saturn (UNIX tutorial) Runs SAS programs on the PC.
Introduction to Statistical Computing in Clinical Research Biostatistics 212 Course director: Mark Pletcher Teaching Assistant: Lee Zane.
Linux+ Guide to Linux Certification, Second Edition
Generating new variables and manipulating data with STATA Biostatistics 212 Session 2.
Getting Started with your data
CCPR Computing Services More Efficient Programming Courtney Engel October 12, 2007.
Day 1: Getting Started Department of Economics
Stata 12 Merging Guide Nathan Favero Texas A&M University October 19, 2012.
ASP.NET Programming with C# and SQL Server First Edition
1 CCPR Computing Services Workshop: Introduction to Stata June, 2006.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1.
Stata Workshop #1 Chiu-Hsieh (Paul) Hsu Associate Professor College of Public Health
Project organisation in Stata Adrian Spoerri and Marcel Zwahlen Department of Social and Preventive Medicine University of Berne, Switzerland Research.
Session I How to use STATA & Basic Data Management Commands.
Introduction to SAS BIO 226 – Spring Outline Windows and common rules Getting the data –The PRINT and CONTENT Procedures Manipulating the data.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
EC501 Gabriella Conti University of Essex
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
API-208: Stata Review Session Daniel Yew Mao Lim Harvard University Spring 2013.
Key Data Management Tasks in Stata
STATA Mini Course Fall 2015 Jane Leber Herr Littauer 113 1Stata Mini Course – Spring 2015.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
L3: BIG STATA CONCEPTS Getting started with Stata Angela Ambroz May 2015.
Linux+ Guide to Linux Certification, Third Edition
Generating new variables and manipulating data with STATA Biostatistics 212 Lecture 3.
1 Lab 2 and Merging Data (with SQL) HRP223 – 2009 October 19, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Statistical Computing in Clinical Research Biostatistics 212.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
STATA for S-052 M. Shane Tutwiler Your Friendly S-040 Lecturer William Johnston IT Services Harvard Graduate School of Education.
1 Stephen L. DesJardins Professor Center for the Study of Higher and Postsecondary Education School of Education and Professor, Gerald R. Ford School of.
Lesson 13 Databases Unit 2—Using the Computer. Computer Concepts BASICS - 22 Objectives Define the purpose and function of database software. Identify.
Summer SAS Workshop Lecture 3. Summer SAS Workshop Website
DTC Quantitative Methods Summary of some SPSS commands Weeks 1 & 2, January 2012.
Today Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation – GOF.
Econometrics-3 XENA BONDARENKO. I. Preparation for Data Analysis a)Create / change working directory b)Specify data c)End Stata d)The four Stata windows.
Linux+ Guide to Linux Certification, Second Edition
Chapter – 8 Software Tools.
Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation –
Stata: Getting Starting and Being Productive with VA Data Give me six hours to chop down a tree and I will spend the first four sharpening the axe. --Abraham.
Ec 2390: Section 1 Useful STATA commands Jack Willis September 14th, 2015.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Before the class starts: 1) login to a computer 2) start Stata 13.
Linux Administration Working with the BASH Shell.
Topics Introduction to Stata – Files / directories – Stata syntax – Useful commands / functions Logistic regression analysis with Stata – Estimation –
LINGO TUTORIAL.
Advanced Quantitative Techniques
Completing the Problem-Solving Process
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Econometrics 704 Emilio Cuilty
ECONOMETRICS ii – spring 2018
Introduction Introduction to Stata 2016.
Introduction to Stata Spring 2017.
Objectives This is an introduction to the statistical software STATA aiming at: Preparing the participants in STATA basics (interphase and commands) for.
Stata Basic Course Lab 4.
Stata Basic Course Lab 2.
Presentation, data and programs at:
Presentation transcript:

1 CCPR Computing Services Introduction to Stata Courtney Engel October 26, 2007

2 Outline Stata  Command Syntax  Basic Commands  Abbreviations  Missing Values  Combining Data  Using do-files  Basic programming  Special Topics  Getting Help  Updating Stata

3 Stata Syntax Basic command syntax: [by varlist:] command [varlist] [= exp] [if exp] [in range] [weighttype=weight] [, options] Brackets = optional portions Italics = user specified

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.

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. Obs #ageagelt

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

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)

8 Keep/Save a Subset of the Data “Keep” a subset of the variables in memory keep make headroom trunk weight length price List variables in current dataset  ds List string variables in current dataset  ds, has(type string) Save current dataset  save autokeep, replace

9 Generating New Variables Create new variable = headroom squared generate headroom2 = headroom^2 Generate numeric from string variable encode 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 Obs #HeadroomHeadroom

10 Generating New Variables, cont. Create categorical variable from continuous variable “price” is integer-valued with minimum 3291 and max Generate categorical version - Method 1: generate priceCat = 0 replace priceCat = 1 if price < 5000 replace priceCat = 2 if price >= 5000 & price < replace priceCat = 3 if price >= & price <.

11 Generating New Variables, cont. Generate categorical version of numerical variable: Method 2 generate priceCat2 = price recode priceCat2 (min/5000 = 1) (5000/10000=2) (10000/max=3) Compare price, priceCat, and priceCat2 table price priceCat table priceCat priceCat2

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

13 Reshape > Wide to Long Wide -> Long: reshape long author, i(year session order) j(count) long - reshape from wide to long author- Stem of the variable going from wide to long i(year session order)- Uniquely identifies an observation in wide form j(count)- Variable which will be created to contain suffix of Author i.e. (1 2) yearSessionOrderAuthor1Author2 2006P013BiddlecomBankole 2006P014AnyaraHinde 2006P015AmouzouBecker Wide format:

14 Reshape > Long to Wide Long -> Wide: reshape wide author, i(year session order) j(count) wide - reshape from long to wide author - variable to be converted from long to wide i(year session order) - variables uniquely identify observations in wide j(count)- variable gives the suffix of Author i.e. (1 2) YearSessionOrderAuthorCount 2006P013Biddlecom1 2006P013Bankole2 2006P014Anyara1 2006P014Hinde2 2006P015Amouzou1 2006P015Becker2 Long format:

15 A few other commands compress - saves data more efficiently sort/ gsort – ascending/descending observation sort order - variable order rename – rename variables set more on/off – produce results with pause?

16 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

17 Missing Values in Stata 8-10 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: “”

18 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 7Stata 8 and later varname !=.varname <. varname ==.varname >=.

19 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

20 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

21 Combining Data Variable _merge (generated by merge and joinby) Pitfalls  Merging unsorted data  Many-to-many using merge instead of joinby _mergeObservation in master dataObservation in “using” data 1YesNo 2 Yes 3

22 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

23 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!

Do-files > Documentation Header *Josie Bruin *HRS project */u/socio/jbruin/HRS/ *October 5, 2007 *Stata version 8 *Purpose: Create and merge two datasets in Stata, * then convert data to SAS *Input programs: *HRS/staprog/H2002.do, *HRS/staprog/x2002.do, *HRS/staprog/mergeFiles.do *Output: *HRS/stalog/H2002.log, *HRS/stalog/x2002.log, *HRS/stalog/mergeFiles.log *HRS/stadata/Hx2002.dta *HRS/sasdata/Hx2002.sas *Special instructions: Check log files for errors *check for duplicates upon new data release File header includes:  Name ( )  Project  Project location  Date  Software Version  Purpose of program  Inputs  Outputs  Special Instructions

25 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

26 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 ///

27 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

28 Saving output Work in do-files and log your sessions! log using filename  replace or append log close Output choices:  *.log file - ASCII file (text)  *.smcl file - nicer format for viewing and printing in Stata

29 Saving Output, cont. Graphs are not saved in log files Export current graph:  graph export graph.ext  Ex: graph export graph.eps Supported formats: .ps,.eps,.wmf,.emf.pict

30 Example using local macro. local mypath "C:\Documents and Settings\MyStata". display `mypath' C:\Documents invalid name r(198);. display C:\Documents and Settings\MyStata C:\Documents invalid name r(198);. display "`mypath'" C:\Documents and Settings\MyStata

31 Example– foreach, return, display foreach var of varlist tenure-ln_wage { quietly summarize `var' local varmean = r(mean) display "Variable `var' has mean `varmean’ " } |tenure hours wks_work ln_wage | | | 1. | | 2. | | 3. | | 4. | | 5. | |

32 Example using forvalues, display forvalues counter = 1/10 { display `counter' } forvalues counter = 0(2)10 { display `counter' }

33 Example: forvalues, generating random variables forvalues j = 1/3 { generate x`j' = uniform() generate y`j' = invnormal(uniform()) } foreach x of varlist x1-x3 y1-y3 { summarize `x' }

34 Example – if/else foreach 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" }

35 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]$") | make | | | | Merc. XR-7 | | Olds Delta 88 |

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

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

38 Stata Resources > 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)

39 Updating Stata help update update all

CCPR’s Cluster and helping your research Software and Data  STATA, SAS, R, Compilers, text editors, etc  HRS, CPS (Unicon version), AddHealth, IFLS, etc Efficiency  Your PC is available for other work when you submit a job to the cluster  Faster processors  More RAM  Easy to share data, programs, etc. with colleagues via the cluster Obtain access by requesting an account 

Questions/Feedback Please me if you need help in the future 