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 Lincoln Todd Wagner June 2007
Outline Getting data into Stata Getting data into Stata Editing in Stata Editing in Stata How does Stata handle data How does Stata handle data Stata notation and help Stata notation and help Using Stata and Basic Stata commands Using Stata and Basic Stata commands
Transferring Data Stattransfer or DBMS copy work Stattransfer or DBMS copy work Stattransfer often seeks to optimize the Stata dataset by default Stattransfer often seeks to optimize the Stata dataset by default –If transferring data with SCRSSN, FORCE Stattransfer to transfer SCRSSN as double precision
Stattransfer CLICK ON DOUBLE
Editing in Stata Any ASCII text editor will work Any ASCII text editor will work Stata has a built in text editor, but it is limited. Stata has a built in text editor, but it is limited. I recommend using another text editor I recommend using another text editorhttp://fmwww.bc.edu/repec/bocode/t/textEditors.html
Handling Data SAS processes one record at a time SAS processes one record at a time Stata processes all the records at the same time Stata processes all the records at the same time –Loops are commonly used in SAS –Loops are very rarely used in Stata
Loading Data into Memory Stata reads the data into memory Stata reads the data into memory –set mem 100m (before you load the data) You must have enough memory for your dataset You must have enough memory for your dataset With large datasets: With large datasets: –drop unnecessary variables –Use the compress command (but don’t compress SCRSSN)
Stata Abbreviations Stata commands can be abbreviated with the first three letters Stata commands can be abbreviated with the first three letters –regression income education female could be written –reg income education female Can also abbreviate variables if uniquely defined Can also abbreviate variables if uniquely defined –reg inc educ fem
Stata Help Stata’s built in help is great Stata’s built in help is great –Help –Help Stata manuals are great because they review theory Stata manuals are great because they review theory
Stata and the Web Stata is “web aware” Stata is “web aware” Check for updates periodically Check for updates periodically –update all You can search for user-written programs You can search for user-written programs –findit output –findit outreg (click to install)
Stata in Windows Page up scrolls through the previous commands Page up scrolls through the previous commands There is a graphical user interface (menus) if you forget a command There is a graphical user interface (menus) if you forget a command We have Stata on rocky and tasha– no graphical capabilities, no menus, and loss of some shortcuts We have Stata on rocky and tasha– no graphical capabilities, no menus, and loss of some shortcuts
Using Stata Create batch files called “.do” files Create batch files called “.do” files I work interactively I work interactively –Run Stata and create do file as I go –I can then use the do file as needed Debugging code and exploratory data analysis is very fast in Stata Debugging code and exploratory data analysis is very fast in Stata
Sysdir, ls and cd Stata recognizes some unix commands, such as ls and cd Stata recognizes some unix commands, such as ls and cd Sysdir provides a listing of Stata’s working directories Sysdir provides a listing of Stata’s working directoriessysdir STATA: C:\Program Files\Stata9\ UPDATES: C:\ProgramFiles\Stata9\ado\updates\ BASE: C:\Program Files\Stata9\ado\base\ SITE: C:\Program Files\Stata9\ado\site\ PLUS: c:\ado\stbplus\ PERSONAL: c:\ado\personal\ OLDPLACE: c:\ado\
Delimiters SAS recognizes “;” as a delimiter SAS recognizes “;” as a delimiter Stata recognizes the carriage return Stata recognizes the carriage return –Always add a carriage return after your last command You can change delimiters to ; You can change delimiters to ; #delimit ;
Missing Data Stata and SAS both use “.” as missing Stata and SAS both use “.” as missing Stata implicitly values a missing as a very large number Stata implicitly values a missing as a very large number SAS implicitly values a missing as a very small number SAS implicitly values a missing as a very small number
Generating and Recoding Variables In SAS you type In SAS you typequality=0; If VA=1 then quality=1; In Stata you type In Stata you type gen quality=0 recode quality 0=1 if VA==1 or replace quality=1 if VA==1
Boolean Logic Stata is picky about Boolean logic Stata is picky about Boolean logic gen y=x if a==b (must use two ==) gen y=x if a>b & b>10 (must use &) gen y=x if a must be before =)
Creating Dummy Variables Goal: create dummy variable for each DRG Goal: create dummy variable for each DRG gen drgnum1=drg==1 or tab drg, gen(drgnum) This second command automatically creates dummy variables This second command automatically creates dummy variables
Drop Drop (drops variables) Drop (drops variables) Drop if X==1 (drop cases where value is 1) Drop if X==1 (drop cases where value is 1)
egen Commands You want to generate total costs for a medical center You want to generate total costs for a medical center In SAS this is done by proc summary In SAS this is done by proc summary In Stata, you can type In Stata, you can type collapse (sum) costs, by (stan3) or sort sta3n by sta3n: egen sumcost=total(cost)
ICD-9 Codes Stata has capabilities to handle ICD-9 diagnosis and procedure codes Stata has capabilities to handle ICD-9 diagnosis and procedure codes You can You can –check to see if codes are valid –generate identifiers based on codes or ranges of codes
Dates Same date functions as SAS Same date functions as SAS
Combining Data Merge Merge –this automatically creates a variable called _merge –merge==1 obs. from master data –merge==2 obs. from only one using dataset –merge==3 obs. from at least two datasets, master or using merge scrssn admitday disday using data_y Append (stacking data) Append (stacking data)
Explicit Subscripting Identify the most recent encounter in an encounter database Identify the most recent encounter in an encounter database gsort id -date by id : gen n=_n by id : gen N=_N gen select=n==1 Ascending sort by ID and reverse by date Record counter from 1 to N per person Total number of records per person
Using Stata
Stata Interface in Windows
Set, Clear and More Set: sets system parameters Set: sets system parameters –Need to set memory size to open a database set mem 100m Clear erases data from memory Clear erases data from memory When output is >1 page, you are asked to continue ( set more off ) When output is >1 page, you are asked to continue ( set more off )
Summarizing Data. sum gender age educ Variable | Obs Mean Std. Dev. Min Max gender | age | educ | Sum, d provides more details on each variable Sum, d provides more details on each variable Tabstat provides summary info, including totals Tabstat provides summary info, including totals
Tabulating Data. tab gender gender | Freq. Percent Cum. gender | Freq. Percent Cum | 2, | 2, | 2, | 2, Total | 4, Total | 4, table gender gender | Freq. gender | Freq | 2,058 1 | 2,058 2 | 2,027 2 | 2,
Tabulating Data tab gender age too many values r(134); tab age gender tab age gender | gender | gender age | 1 2 | Total age | 1 2 | Total | | | | | | | | 143… 94 | 1 0 | 1 94 | 1 0 | Total | 2,058 2,027 | 4,085 Total | 2,058 2,027 | 4,085
Tabstat. tabstat age, by (gender) gender | mean gender | mean | | | | Total | Total | table gender, c(mean age) gender | mean(age) gender | mean(age) | | | |
Graphing Diagnostic graphics Diagnostic graphics Presenting Presentingresults
Basic Analytical Functions OLS (reg) OLS (reg) Logistic, probit, count data (e.g., CLAD) Logistic, probit, count data (e.g., CLAD) Multinomials Multinomials GLM/HLM GLM/HLM Duration models Duration models Semi and non-parametric models Semi and non-parametric models
Output Linear regressionNumber of obs= 1306 F( 21, 1284)= Prob > F= R-squared= Root MSE= Robust wtp Coef.Std. Err.tP>t[95% Conf.Interval] ethn Ethn ethn ethn english lifeus age income incmis _cons
Outreg Outputs data to a delimited file Outputs data to a delimited file Delimited file can be read into Excel Delimited file can be read into Excel Very flexible Very flexible Creates publishable tables Creates publishable tables