SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
Introduction to Stata

                           Ista Zahn

                               IQSS


                    Friday February 8, 2013



                     The Institute
                     for Quantitative Social Science
                     at Harvard University



Ista Zahn (IQSS)        Introduction to Stata   Friday February 8, 2013   1 / 37
Outline


   1   Introduction

   2   Getting data into Stata

   3   Statistics and graphs

   4   Basic data management

   5   Wrap-up




   Ista Zahn (IQSS)            Introduction to Stata   Friday February 8, 2013   2 / 37
Introduction

Topic


   1    Introduction

   2    Getting data into Stata

   3    Statistics and graphs

   4    Basic data management

   5    Wrap-up




   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   3 / 37
Introduction

Documents for today




   USERNAME: dataclass PASSWORD: dataclass
          Find class materials at: Scratch > StataIntro
          FIRST THING: copy this folder to your desktop!




   Ista Zahn (IQSS)            Introduction to Stata   Friday February 8, 2013   4 / 37
Introduction

Organization



          Please feel free to ask questions at any point if they are
          relevant to the current topic (or if you are lost!)

          Collaboration with your neighbors is encouraged
          If you are using a laptop, you will need to adjust paths
          accordingly
          Make comments in your Do-file rather than on hand-outs
                save on flash drive or email to yourself




   Ista Zahn (IQSS)              Introduction to Stata    Friday February 8, 2013   5 / 37
Introduction

Workshop descripton



          This is an introduction to Stata
          Assumes no/very little knowledge of Stata
          Not appropriate for people already well familiar with Stata
          Learning Objectives:
                Familiarize yourself with the Stata interface
                Get data in and out of Stata
                Compute statistics and construct graphical displays
                Compute new variables and transformations




   Ista Zahn (IQSS)              Introduction to Stata   Friday February 8, 2013   6 / 37
Introduction

Why stata?




          Used in a variety of disciplines
          User-friendly
          Great guides available on web (as well as in HMDC
          computer lab library)
          Student and other discount packages available at
          reasonable cost




   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   7 / 37
Introduction

Stata interface




          Review and Variable windows can be closed (user
          preference)
          Command window can be shortened (recommended)

   Ista Zahn (IQSS)           Introduction to Stata   Friday February 8, 2013   8 / 37
Introduction

Do-files


          You can type all the same commands into the Do-file that
          you would type into the command window
          BUT. . . the Do-file allows you to save your commands
          Your Do-file should contain ALL commands you executed –
          at least all the “correct” commands!
          I recommend never using the command window or menus
          to make CHANGES to data
          Saving commands in Do-file allows you to keep a written
          record of everything you have done to your data
                Allows easy replication
                Allows you to go back and re-run commands, analyses and
                make modifications



   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   9 / 37
Introduction

Stata help




          Easiest way to get help in Stata - just type help followed by
          topic or command, e.g., help regress
          Falls back to “search” if command not found
          Generally, if you google “Stata [topic],” you’ll get some
          helpful hits
          UCLA website: http://www.ats.ucla.edu/stat/Stata/




   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   10 / 37
Introduction

General Stata command syntax




          Most Stata commands follow the same underlying principles
          Command varlist, options, e.g., sum var1 var2, detail
                CAUTION - in some cases, if you type a command and
                don’t specify a variable, Stata will perform the command
                on all variables in your dataset
          You can find command-specific syntax in the help files




   Ista Zahn (IQSS)              Introduction to Stata   Friday February 8, 2013   11 / 37
Introduction

Commenting and formatting syntax

          Start with comment describing your Do-file and use
          comments throughout
          Single line and block comments

          // comment
           describe var
           /*
           comment block comment block comment block comment
           block comment block comment block
          */

          Use /// to break varlists over multiple lines:

          // break commands over multible lines
          describe var1 var2 var2 ///
          var4 var5 var6


   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   12 / 37
Introduction

Let’s get started



          Launch the Stata program (MP or SE, does not matter
          unless doing computationally intensive work)
                Open up a new Do-file
                Run our first Stata code!

          // change directory
          cd "C://Users/dataclass/Desktop/StataIntro"
          // start a log file to record your stata session
          log using myStataLog, replace
          // Pause / resume logging with "log on" / "log off"




   Ista Zahn (IQSS)              Introduction to Stata   Friday February 8, 2013   13 / 37
Introduction

How to start every do-file




      1   Describe what the file does
      2   Change directory
      3   Begin log file
      4   Call up data
      5   Save data under new name (if making changes to dataset)




   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   14 / 37
Getting data into Stata

Topic


   1    Introduction

   2    Getting data into Stata

   3    Statistics and graphs

   4    Basic data management

   5    Wrap-up




   Ista Zahn (IQSS)                  Introduction to Stata   Friday February 8, 2013   15 / 37
Getting data into Stata

Data file commands




          Next, we want to open our data file
          Open/save data sets with “use” and “save”:

          // open the gss.dta data set
          use dataSets/gss.dta
          // saving your data file:
          save newgss.dta, replace
          /* the "replace" option tells stata it’s OK to
             write over an existing file */




   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   16 / 37
Getting data into Stata

A note about path names




          If your path has no spaces in the name (that means all
          directories, folders, file names, etc. can have no spaces),
          you can write the path as is
          If there are spaces, you need to put your pathname in
          quotes
          Best to get in the habit of quoting paths




   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   17 / 37
Getting data into Stata

Where’s my data?




          Data editor (browse)
          Data editor (edit)
                Using the data editor is discouraged (why?)
          Always keep any changes to your data in your Do-file
          Avoid temptation of making manual changes by viewing
          data via the browser rather than editor




   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   18 / 37
Getting data into Stata

What if my data is not a Stata file?


          Import delimited text files

          /* import data from a .csv file */
          insheet using gss.csv, clear
          /* save data to a .csv file */
          outsheet using gss_new.csv, replace comma

          Import data from SAS and Excel

          /* import/export SAS xport files */
          import sasxport gss.xpt
          export sasxport newFileName

          /* import/export data from Excel */
          import excel using gss.xls, firstrow
          export excel newFileName.xls



   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   19 / 37
Getting data into Stata

What if my data is from another statistical software
program?



          SPSS/PASW will allow you to save your data as a Stata
          file
                Go to: file > save as > Stata (use most recent version
                available)
                Then you can just go into Stata and open it
          Another option is StatTransfer, a program that converts
          data from/to many common formats, including SAS, SPSS,
          Stata, and many more




   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   20 / 37
Getting data into Stata

Exercise 1: Importing data



      1   Close down Stata and open a new session
      2   Go through the three steps for starting each Stata session
          that we reviewed
                Begin a log file
                Open your Stata dataset (gss.dta)
                Save your Stata dataset using a different name
      3   Try opening the following files:
                A comma separated value file: gss.csv
                A SPSS file: gss.sav
                A SAS transport file: gss.xpt




   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   21 / 37
Statistics and graphs

Topic


   1    Introduction

   2    Getting data into Stata

   3    Statistics and graphs

   4    Basic data management

   5    Wrap-up




   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   22 / 37
Statistics and graphs

Frequently used commands

          Commands for reviewing and inspecting data:
                describe // labels, storage type etc.
                sum // statistical summary (mean, sd, min/max etc.)
                codebook // storage type, unique values, labels
                list // print actuall values
                tab // (cross) tabulate variables
                browse // view the data in a spreadsheet-like window
          Examples

           /* commands useful for inspecting data */
          sum educ // statistical summary of education
          codebook region // information about how region is coded
          tab sex // numbers of male and female participants

          Remember, if you run these commands without specifying
          variables, Stata will produce output for every variable

   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   23 / 37
Statistics and graphs

Basic graphing commands


          Univariate distribution(s) using hist

           /* Histograms */
           hist educ
           /* Interested in normality of your data? You can tell
          Stata to draw the normal curve over your histogram*/
           hist age, normal

          View bivariate distributions with scatterplots

          /* scatterplots */
          twoway (scatter educ age)
          graph matrix educ age inc




   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   24 / 37
Statistics and graphs

The “by” command



          Sometimes, you’d like to generate output based on different
          categories of a grouping variable
          The “by” command does just this

          /* tabulate happy separately for men and women */
          bysort sex: tab happy
          /* not all commands can be used with the by prefix.
             some, (like hist) have a "by" option instead */
          hist happy, by(sex)




   Ista Zahn (IQSS)               Introduction to Stata   Friday February 8, 2013   25 / 37
Statistics and graphs

Exercise 2: Descriptive statistics

      1   Use the dataset, gss.dta
      2   Examine a few selected variables using the describe, sum
          and codebook commands
      3   Tabulate the variable, “marital,” with and without labels
      4   Summarize the variable, “income” separately participants
          based on marital status
      5   Cross-tabulate marital with region and show gender percent
          by region
      6   Summarize the variable, “happy” for married individuals only
      7   Generate a histogram of income
      8   Generate a second histogram of income, but this time, split
          income based on participants sex and ask Stata to print the
          normal curve on your histograms

   Ista Zahn (IQSS)               Introduction to Stata   Friday February 8, 2013   26 / 37
Basic data management

Topic


   1    Introduction

   2    Getting data into Stata

   3    Statistics and graphs

   4    Basic data management

   5    Wrap-up




   Ista Zahn (IQSS)                 Introduction to Stata   Friday February 8, 2013   27 / 37
Basic data management

Labels




          You never know why and when your data may be reviewed
          ALWAYS label every variable no matter how insignificant it
          may seem
          Stata uses two sets of labels: variable labels and value
          labels
          Variable labels are very easy to use – value labels are a little
          more complicated




   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   28 / 37
Basic data management

Variable and value labels
          Variable labels
          /* Label variable inc "household income" */
          label var inc "household income"

          /* Want to change the name of your variable? */
          rename oldvarname newvarname
          Value labels are a two step process: define a value label,
          then assign defined label to variable(s)

          /*define a value label for sex */
          label define mySexLabel 1 "Male" 2 "Female"
          /* assign our "example" label to var1 through var3 */
          label val sex mySexLabel
          /* Label define particularly useful when you have
            multiple variables with the same value structure */
          /* If you have many variables, you can search labels
             using lookfor */
          lookfor income
   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   29 / 37
Basic data management

Exercise 3: Variable labels and value labels
      1   Open the data set gss.csv
      2   Familiarize yourself with the data using describe, sum, etc.
      3   Rename and label variables using the following codebook:
                        var   rename to       label with
                        v1    marital         marital status
                        v2    age             age of respondent
                        v3    educ            education
                        v4    sex             respondent’s sex
                        v5    inc             household income
                        v6    happy           general happiness
                        v7    region          region of interview

      1   Add value labels to your “marital” variable using this
          codebook:
                                value    label
                                    1    “married”
                                    2    “widowed”
                                    3    “divorced”
                                    4    “separated”
                                    5    “never married”

   Ista Zahn (IQSS)                Introduction to Stata        Friday February 8, 2013   30 / 37
Basic data management

Working on subsets

          It is often useful to select just those rows of your data
          where some condition holds–for example select only rows
          where sex is 1 (male)
          The following operators allow you to do this:
                   == equal to
                    != not equal to
                     > greater than
                     < less than
                   >= greater than or equal to
                   <= less than or equal to
                     & and
                      | or
          Note the double equals signs for testing equality


   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   31 / 37
Basic data management

Generating and replacing variables
          Create new variables using “gen”
          /* create a new variable named mc_inc"
             equal to inc minus the mean of inc */
          gen mc_inc = inc - 15.37

          Sometimes useful to start with blank values and fill them in
          based on values of existing variables
          /* generate a column of missings */
          gen age_wealth = .
          /* Next, start adding your qualifications */
          replace age_wealth=1 if age<30 & inc < 10
          replace age_wealth=2 if age<30 & inc > 10
          replace age_wealth=3 if age>30 & inc < 10
          replace age_wealth=4 if age>30 & inc > 10

          /* conditions can also be combined with "or" */
          gen young=0
          replace young=1 if age_wealth==1 | age_wealth==2
   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   32 / 37
Basic data management

Recoding, dropping variables




          Recoding variables

          /* recode happy into sad */
          recode happy (1=3) (3=1), gen(sad)

          Deleting variables

          drop region // delete region
          keep age-inc // keep age, educ, sex, and inc




   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   33 / 37
Basic data management

Exercise 4: Manipulating variables



      1   Use the dataset, gss.dta
      2   Generate a new variable, age2 equal to age squared
      3   Generate a new “high income” variable that will take on a
          value of “1” if a person has an income value greater than
          “15” and “0” otherwise
      4   Generate a new divorced/separated dummy variable that
          will take on a value of “1” if a person is either divorced or
          separated and “0” otherwise




   Ista Zahn (IQSS)                Introduction to Stata   Friday February 8, 2013   34 / 37
Wrap-up

Topic


   1    Introduction

   2    Getting data into Stata

   3    Statistics and graphs

   4    Basic data management

   5    Wrap-up




   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   35 / 37
Wrap-up

Help us make this workshop better!




          Please take a moment to fill out a very short feedback form
          These workshops exist for you – tell us what you need!
          http://tinyurl.com/6h3cxnz




   Ista Zahn (IQSS)           Introduction to Stata   Friday February 8, 2013   36 / 37
Wrap-up

Additional resources


          IQSS workshops:
          http://projects.iq.harvard.edu/rtc/filter_by/workshops
          IQSS statistical consulting: http://rtc.iq.harvard.edu
          The RCE
                Research Computing Enviroment (RCE) service available to
                Harvard & MIT users
                www.iq.harvard.edu/research_computing
                Wonderful resource for organizing data, running analyses
                efficiently
                Creates a centralized place to store data and run analysis
                Supplies persistent desktop environment accessible from
                any computer with an internet connection




   Ista Zahn (IQSS)             Introduction to Stata   Friday February 8, 2013   37 / 37

Weitere ähnliche Inhalte

Was ist angesagt?

An introduction to spss
An introduction to spssAn introduction to spss
An introduction to spsszeeshanwrch
 
Stata Training_EEA.ppt
Stata Training_EEA.pptStata Training_EEA.ppt
Stata Training_EEA.pptselam49
 
"A basic guide to SPSS"
"A basic guide to SPSS""A basic guide to SPSS"
"A basic guide to SPSS"Bashir7576
 
Categorical data analysis
Categorical data analysisCategorical data analysis
Categorical data analysisSumit Das
 
What is statistics
What is statisticsWhat is statistics
What is statisticsRaj Teotia
 
Linear regression
Linear regressionLinear regression
Linear regressionTech_MX
 
Introduction to STATA(2).pdf
Introduction to STATA(2).pdfIntroduction to STATA(2).pdf
Introduction to STATA(2).pdfYomif3
 
Introduction to Statistics and Statistical Inference
Introduction to Statistics and Statistical InferenceIntroduction to Statistics and Statistical Inference
Introduction to Statistics and Statistical InferenceJezhabeth Villegas
 
Descriptive statistics
Descriptive statisticsDescriptive statistics
Descriptive statisticsAiden Yeh
 
Introduction to Statistics - Basic concepts
Introduction to Statistics - Basic conceptsIntroduction to Statistics - Basic concepts
Introduction to Statistics - Basic conceptsDocIbrahimAbdelmonaem
 
Introduction to Statistics
Introduction to StatisticsIntroduction to Statistics
Introduction to Statisticsaan786
 

Was ist angesagt? (20)

An introduction to spss
An introduction to spssAn introduction to spss
An introduction to spss
 
Stata Training_EEA.ppt
Stata Training_EEA.pptStata Training_EEA.ppt
Stata Training_EEA.ppt
 
"A basic guide to SPSS"
"A basic guide to SPSS""A basic guide to SPSS"
"A basic guide to SPSS"
 
Categorical data analysis
Categorical data analysisCategorical data analysis
Categorical data analysis
 
What is statistics
What is statisticsWhat is statistics
What is statistics
 
Data management through spss
Data management through spssData management through spss
Data management through spss
 
Time series
Time seriesTime series
Time series
 
Univariate Analysis
Univariate AnalysisUnivariate Analysis
Univariate Analysis
 
Linear regression
Linear regressionLinear regression
Linear regression
 
Introduction to spss
Introduction to spssIntroduction to spss
Introduction to spss
 
SPSS
SPSSSPSS
SPSS
 
Introduction to STATA(2).pdf
Introduction to STATA(2).pdfIntroduction to STATA(2).pdf
Introduction to STATA(2).pdf
 
(Manual spss)
(Manual spss)(Manual spss)
(Manual spss)
 
Introduction to Statistics and Statistical Inference
Introduction to Statistics and Statistical InferenceIntroduction to Statistics and Statistical Inference
Introduction to Statistics and Statistical Inference
 
Descriptive statistics
Descriptive statisticsDescriptive statistics
Descriptive statistics
 
Spss an introduction
Spss  an introductionSpss  an introduction
Spss an introduction
 
Introduction to Statistics - Basic concepts
Introduction to Statistics - Basic conceptsIntroduction to Statistics - Basic concepts
Introduction to Statistics - Basic concepts
 
Introduction to Statistics
Introduction to StatisticsIntroduction to Statistics
Introduction to Statistics
 
Spss training notes
Spss training notesSpss training notes
Spss training notes
 
Data analysis
Data analysisData analysis
Data analysis
 

Andere mochten auch

STATA - Download Examples
STATA - Download ExamplesSTATA - Download Examples
STATA - Download Examplesstata_org_uk
 
Statistical Package for Social Science (SPSS)
Statistical Package for Social Science (SPSS)Statistical Package for Social Science (SPSS)
Statistical Package for Social Science (SPSS)sspink
 
STATA - Time Series Analysis
STATA - Time Series AnalysisSTATA - Time Series Analysis
STATA - Time Series Analysisstata_org_uk
 
Spss lecture notes
Spss lecture notesSpss lecture notes
Spss lecture notesDavid mbwiga
 
Workshop on Introduction to eviews
Workshop on Introduction to eviewsWorkshop on Introduction to eviews
Workshop on Introduction to eviewsDr. Vignes Gopal
 
STATA - Importing Data
STATA - Importing DataSTATA - Importing Data
STATA - Importing Datastata_org_uk
 
Eviews
EviewsEviews
EviewsYAMISO
 
Data Collection and Analysis Tools
Data Collection and Analysis ToolsData Collection and Analysis Tools
Data Collection and Analysis ToolsCaren Gamboa
 
STATA - Instrumental Variables
STATA - Instrumental VariablesSTATA - Instrumental Variables
STATA - Instrumental Variablesstata_org_uk
 
STATA - Panel Regressions
STATA - Panel RegressionsSTATA - Panel Regressions
STATA - Panel Regressionsstata_org_uk
 
Introduction To SPSS
Introduction To SPSSIntroduction To SPSS
Introduction To SPSSPhi Jack
 
Rasch Model Theorem_Scale Construct
Rasch Model Theorem_Scale ConstructRasch Model Theorem_Scale Construct
Rasch Model Theorem_Scale ConstructSaidfudin Mas'udi
 
Material 1 introduction to eviews
Material 1 introduction to eviewsMaterial 1 introduction to eviews
Material 1 introduction to eviewsDr. Vignes Gopal
 
Eviews
EviewsEviews
EviewsYaSoJi
 
060 techniques of_data_analysis
060 techniques of_data_analysis060 techniques of_data_analysis
060 techniques of_data_analysisvsimkhed
 
1a Introduction To Rasch Measurement Model Msm
1a Introduction To Rasch Measurement Model Msm1a Introduction To Rasch Measurement Model Msm
1a Introduction To Rasch Measurement Model MsmSaidfudin Mas'udi
 

Andere mochten auch (20)

STATA - Download Examples
STATA - Download ExamplesSTATA - Download Examples
STATA - Download Examples
 
Statistical Package for Social Science (SPSS)
Statistical Package for Social Science (SPSS)Statistical Package for Social Science (SPSS)
Statistical Package for Social Science (SPSS)
 
STATA - Time Series Analysis
STATA - Time Series AnalysisSTATA - Time Series Analysis
STATA - Time Series Analysis
 
Spss lecture notes
Spss lecture notesSpss lecture notes
Spss lecture notes
 
Workshop on Introduction to eviews
Workshop on Introduction to eviewsWorkshop on Introduction to eviews
Workshop on Introduction to eviews
 
STATA - Importing Data
STATA - Importing DataSTATA - Importing Data
STATA - Importing Data
 
Tutorial1 eviews_basics
Tutorial1 eviews_basicsTutorial1 eviews_basics
Tutorial1 eviews_basics
 
Eviews
EviewsEviews
Eviews
 
Data Collection and Analysis Tools
Data Collection and Analysis ToolsData Collection and Analysis Tools
Data Collection and Analysis Tools
 
STATA - Instrumental Variables
STATA - Instrumental VariablesSTATA - Instrumental Variables
STATA - Instrumental Variables
 
STATA - Panel Regressions
STATA - Panel RegressionsSTATA - Panel Regressions
STATA - Panel Regressions
 
Introduction To SPSS
Introduction To SPSSIntroduction To SPSS
Introduction To SPSS
 
Spss
SpssSpss
Spss
 
Eviews 7
Eviews 7Eviews 7
Eviews 7
 
Rasch Model Theorem_Scale Construct
Rasch Model Theorem_Scale ConstructRasch Model Theorem_Scale Construct
Rasch Model Theorem_Scale Construct
 
Material 1 introduction to eviews
Material 1 introduction to eviewsMaterial 1 introduction to eviews
Material 1 introduction to eviews
 
Eviews
EviewsEviews
Eviews
 
060 techniques of_data_analysis
060 techniques of_data_analysis060 techniques of_data_analysis
060 techniques of_data_analysis
 
1a Introduction To Rasch Measurement Model Msm
1a Introduction To Rasch Measurement Model Msm1a Introduction To Rasch Measurement Model Msm
1a Introduction To Rasch Measurement Model Msm
 
Impact evaluation an overview
Impact evaluation  an overviewImpact evaluation  an overview
Impact evaluation an overview
 

Ähnlich wie Introduction to Stata

Introduction to SAS
Introduction to SASIntroduction to SAS
Introduction to SASizahn
 
Topic 4 intro spss_stata
Topic 4 intro spss_stataTopic 4 intro spss_stata
Topic 4 intro spss_stataSM Lalon
 
Evaluation Spss
Evaluation SpssEvaluation Spss
Evaluation Spssjackng
 
Repository Fringe 2016 - Survey Documentation and Analysis
Repository Fringe 2016 - Survey Documentation and AnalysisRepository Fringe 2016 - Survey Documentation and Analysis
Repository Fringe 2016 - Survey Documentation and AnalysisEDINA, University of Edinburgh
 
Analytics with SAS
Analytics with SASAnalytics with SAS
Analytics with SASEdureka!
 
SPSS vs Stata: The Best Ever Comparison
SPSS vs Stata: The Best Ever ComparisonSPSS vs Stata: The Best Ever Comparison
SPSS vs Stata: The Best Ever ComparisonStat Analytica
 
SPSS vs Stata: All You need to Know
SPSS vs Stata: All You need to KnowSPSS vs Stata: All You need to Know
SPSS vs Stata: All You need to KnowStat Analytica
 
Radyakin usespss
Radyakin usespssRadyakin usespss
Radyakin usespssfjolla_00
 
Stata datman
Stata datmanStata datman
Stata datmanizahn
 
Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8thotakoti
 
SPSS introduction Presentation
SPSS introduction Presentation SPSS introduction Presentation
SPSS introduction Presentation befikra
 
Important SAS Tips and Tricks for A Grade
Important SAS Tips and Tricks for A GradeImportant SAS Tips and Tricks for A Grade
Important SAS Tips and Tricks for A GradeLesa Cote
 
Skutil - H2O meets Sklearn - Taylor Smith
Skutil - H2O meets Sklearn - Taylor SmithSkutil - H2O meets Sklearn - Taylor Smith
Skutil - H2O meets Sklearn - Taylor SmithSri Ambati
 

Ähnlich wie Introduction to Stata (20)

Introduction to SAS
Introduction to SASIntroduction to SAS
Introduction to SAS
 
StataTutorial.pdf
StataTutorial.pdfStataTutorial.pdf
StataTutorial.pdf
 
Topic 4 intro spss_stata
Topic 4 intro spss_stataTopic 4 intro spss_stata
Topic 4 intro spss_stata
 
Sas summary guide
Sas summary guideSas summary guide
Sas summary guide
 
SAS Programming Notes
SAS Programming NotesSAS Programming Notes
SAS Programming Notes
 
Radyakin usespss
Radyakin usespssRadyakin usespss
Radyakin usespss
 
SPSS
SPSSSPSS
SPSS
 
Evaluation Spss
Evaluation SpssEvaluation Spss
Evaluation Spss
 
Repository Fringe 2016 - Survey Documentation and Analysis
Repository Fringe 2016 - Survey Documentation and AnalysisRepository Fringe 2016 - Survey Documentation and Analysis
Repository Fringe 2016 - Survey Documentation and Analysis
 
Analytics with SAS
Analytics with SASAnalytics with SAS
Analytics with SAS
 
SAS Commands
SAS CommandsSAS Commands
SAS Commands
 
SPSS vs Stata: The Best Ever Comparison
SPSS vs Stata: The Best Ever ComparisonSPSS vs Stata: The Best Ever Comparison
SPSS vs Stata: The Best Ever Comparison
 
SPSS vs Stata: All You need to Know
SPSS vs Stata: All You need to KnowSPSS vs Stata: All You need to Know
SPSS vs Stata: All You need to Know
 
Radyakin usespss
Radyakin usespssRadyakin usespss
Radyakin usespss
 
Stata datman
Stata datmanStata datman
Stata datman
 
SAS Online Training by Real Time Working Professionals in USA,UK,India,Middle...
SAS Online Training by Real Time Working Professionals in USA,UK,India,Middle...SAS Online Training by Real Time Working Professionals in USA,UK,India,Middle...
SAS Online Training by Real Time Working Professionals in USA,UK,India,Middle...
 
Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8
 
SPSS introduction Presentation
SPSS introduction Presentation SPSS introduction Presentation
SPSS introduction Presentation
 
Important SAS Tips and Tricks for A Grade
Important SAS Tips and Tricks for A GradeImportant SAS Tips and Tricks for A Grade
Important SAS Tips and Tricks for A Grade
 
Skutil - H2O meets Sklearn - Taylor Smith
Skutil - H2O meets Sklearn - Taylor SmithSkutil - H2O meets Sklearn - Taylor Smith
Skutil - H2O meets Sklearn - Taylor Smith
 

Kürzlich hochgeladen

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Kürzlich hochgeladen (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Introduction to Stata

  • 1. Introduction to Stata Ista Zahn IQSS Friday February 8, 2013 The Institute for Quantitative Social Science at Harvard University Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 1 / 37
  • 2. Outline 1 Introduction 2 Getting data into Stata 3 Statistics and graphs 4 Basic data management 5 Wrap-up Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 2 / 37
  • 3. Introduction Topic 1 Introduction 2 Getting data into Stata 3 Statistics and graphs 4 Basic data management 5 Wrap-up Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 3 / 37
  • 4. Introduction Documents for today USERNAME: dataclass PASSWORD: dataclass Find class materials at: Scratch > StataIntro FIRST THING: copy this folder to your desktop! Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 4 / 37
  • 5. Introduction Organization Please feel free to ask questions at any point if they are relevant to the current topic (or if you are lost!) Collaboration with your neighbors is encouraged If you are using a laptop, you will need to adjust paths accordingly Make comments in your Do-file rather than on hand-outs save on flash drive or email to yourself Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 5 / 37
  • 6. Introduction Workshop descripton This is an introduction to Stata Assumes no/very little knowledge of Stata Not appropriate for people already well familiar with Stata Learning Objectives: Familiarize yourself with the Stata interface Get data in and out of Stata Compute statistics and construct graphical displays Compute new variables and transformations Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 6 / 37
  • 7. Introduction Why stata? Used in a variety of disciplines User-friendly Great guides available on web (as well as in HMDC computer lab library) Student and other discount packages available at reasonable cost Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 7 / 37
  • 8. Introduction Stata interface Review and Variable windows can be closed (user preference) Command window can be shortened (recommended) Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 8 / 37
  • 9. Introduction Do-files You can type all the same commands into the Do-file that you would type into the command window BUT. . . the Do-file allows you to save your commands Your Do-file should contain ALL commands you executed – at least all the “correct” commands! I recommend never using the command window or menus to make CHANGES to data Saving commands in Do-file allows you to keep a written record of everything you have done to your data Allows easy replication Allows you to go back and re-run commands, analyses and make modifications Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 9 / 37
  • 10. Introduction Stata help Easiest way to get help in Stata - just type help followed by topic or command, e.g., help regress Falls back to “search” if command not found Generally, if you google “Stata [topic],” you’ll get some helpful hits UCLA website: http://www.ats.ucla.edu/stat/Stata/ Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 10 / 37
  • 11. Introduction General Stata command syntax Most Stata commands follow the same underlying principles Command varlist, options, e.g., sum var1 var2, detail CAUTION - in some cases, if you type a command and don’t specify a variable, Stata will perform the command on all variables in your dataset You can find command-specific syntax in the help files Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 11 / 37
  • 12. Introduction Commenting and formatting syntax Start with comment describing your Do-file and use comments throughout Single line and block comments // comment describe var /* comment block comment block comment block comment block comment block comment block */ Use /// to break varlists over multiple lines: // break commands over multible lines describe var1 var2 var2 /// var4 var5 var6 Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 12 / 37
  • 13. Introduction Let’s get started Launch the Stata program (MP or SE, does not matter unless doing computationally intensive work) Open up a new Do-file Run our first Stata code! // change directory cd "C://Users/dataclass/Desktop/StataIntro" // start a log file to record your stata session log using myStataLog, replace // Pause / resume logging with "log on" / "log off" Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 13 / 37
  • 14. Introduction How to start every do-file 1 Describe what the file does 2 Change directory 3 Begin log file 4 Call up data 5 Save data under new name (if making changes to dataset) Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 14 / 37
  • 15. Getting data into Stata Topic 1 Introduction 2 Getting data into Stata 3 Statistics and graphs 4 Basic data management 5 Wrap-up Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 15 / 37
  • 16. Getting data into Stata Data file commands Next, we want to open our data file Open/save data sets with “use” and “save”: // open the gss.dta data set use dataSets/gss.dta // saving your data file: save newgss.dta, replace /* the "replace" option tells stata it’s OK to write over an existing file */ Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 16 / 37
  • 17. Getting data into Stata A note about path names If your path has no spaces in the name (that means all directories, folders, file names, etc. can have no spaces), you can write the path as is If there are spaces, you need to put your pathname in quotes Best to get in the habit of quoting paths Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 17 / 37
  • 18. Getting data into Stata Where’s my data? Data editor (browse) Data editor (edit) Using the data editor is discouraged (why?) Always keep any changes to your data in your Do-file Avoid temptation of making manual changes by viewing data via the browser rather than editor Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 18 / 37
  • 19. Getting data into Stata What if my data is not a Stata file? Import delimited text files /* import data from a .csv file */ insheet using gss.csv, clear /* save data to a .csv file */ outsheet using gss_new.csv, replace comma Import data from SAS and Excel /* import/export SAS xport files */ import sasxport gss.xpt export sasxport newFileName /* import/export data from Excel */ import excel using gss.xls, firstrow export excel newFileName.xls Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 19 / 37
  • 20. Getting data into Stata What if my data is from another statistical software program? SPSS/PASW will allow you to save your data as a Stata file Go to: file > save as > Stata (use most recent version available) Then you can just go into Stata and open it Another option is StatTransfer, a program that converts data from/to many common formats, including SAS, SPSS, Stata, and many more Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 20 / 37
  • 21. Getting data into Stata Exercise 1: Importing data 1 Close down Stata and open a new session 2 Go through the three steps for starting each Stata session that we reviewed Begin a log file Open your Stata dataset (gss.dta) Save your Stata dataset using a different name 3 Try opening the following files: A comma separated value file: gss.csv A SPSS file: gss.sav A SAS transport file: gss.xpt Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 21 / 37
  • 22. Statistics and graphs Topic 1 Introduction 2 Getting data into Stata 3 Statistics and graphs 4 Basic data management 5 Wrap-up Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 22 / 37
  • 23. Statistics and graphs Frequently used commands Commands for reviewing and inspecting data: describe // labels, storage type etc. sum // statistical summary (mean, sd, min/max etc.) codebook // storage type, unique values, labels list // print actuall values tab // (cross) tabulate variables browse // view the data in a spreadsheet-like window Examples /* commands useful for inspecting data */ sum educ // statistical summary of education codebook region // information about how region is coded tab sex // numbers of male and female participants Remember, if you run these commands without specifying variables, Stata will produce output for every variable Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 23 / 37
  • 24. Statistics and graphs Basic graphing commands Univariate distribution(s) using hist /* Histograms */ hist educ /* Interested in normality of your data? You can tell Stata to draw the normal curve over your histogram*/ hist age, normal View bivariate distributions with scatterplots /* scatterplots */ twoway (scatter educ age) graph matrix educ age inc Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 24 / 37
  • 25. Statistics and graphs The “by” command Sometimes, you’d like to generate output based on different categories of a grouping variable The “by” command does just this /* tabulate happy separately for men and women */ bysort sex: tab happy /* not all commands can be used with the by prefix. some, (like hist) have a "by" option instead */ hist happy, by(sex) Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 25 / 37
  • 26. Statistics and graphs Exercise 2: Descriptive statistics 1 Use the dataset, gss.dta 2 Examine a few selected variables using the describe, sum and codebook commands 3 Tabulate the variable, “marital,” with and without labels 4 Summarize the variable, “income” separately participants based on marital status 5 Cross-tabulate marital with region and show gender percent by region 6 Summarize the variable, “happy” for married individuals only 7 Generate a histogram of income 8 Generate a second histogram of income, but this time, split income based on participants sex and ask Stata to print the normal curve on your histograms Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 26 / 37
  • 27. Basic data management Topic 1 Introduction 2 Getting data into Stata 3 Statistics and graphs 4 Basic data management 5 Wrap-up Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 27 / 37
  • 28. Basic data management Labels You never know why and when your data may be reviewed ALWAYS label every variable no matter how insignificant it may seem Stata uses two sets of labels: variable labels and value labels Variable labels are very easy to use – value labels are a little more complicated Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 28 / 37
  • 29. Basic data management Variable and value labels Variable labels /* Label variable inc "household income" */ label var inc "household income" /* Want to change the name of your variable? */ rename oldvarname newvarname Value labels are a two step process: define a value label, then assign defined label to variable(s) /*define a value label for sex */ label define mySexLabel 1 "Male" 2 "Female" /* assign our "example" label to var1 through var3 */ label val sex mySexLabel /* Label define particularly useful when you have multiple variables with the same value structure */ /* If you have many variables, you can search labels using lookfor */ lookfor income Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 29 / 37
  • 30. Basic data management Exercise 3: Variable labels and value labels 1 Open the data set gss.csv 2 Familiarize yourself with the data using describe, sum, etc. 3 Rename and label variables using the following codebook: var rename to label with v1 marital marital status v2 age age of respondent v3 educ education v4 sex respondent’s sex v5 inc household income v6 happy general happiness v7 region region of interview 1 Add value labels to your “marital” variable using this codebook: value label 1 “married” 2 “widowed” 3 “divorced” 4 “separated” 5 “never married” Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 30 / 37
  • 31. Basic data management Working on subsets It is often useful to select just those rows of your data where some condition holds–for example select only rows where sex is 1 (male) The following operators allow you to do this: == equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to & and | or Note the double equals signs for testing equality Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 31 / 37
  • 32. Basic data management Generating and replacing variables Create new variables using “gen” /* create a new variable named mc_inc" equal to inc minus the mean of inc */ gen mc_inc = inc - 15.37 Sometimes useful to start with blank values and fill them in based on values of existing variables /* generate a column of missings */ gen age_wealth = . /* Next, start adding your qualifications */ replace age_wealth=1 if age<30 & inc < 10 replace age_wealth=2 if age<30 & inc > 10 replace age_wealth=3 if age>30 & inc < 10 replace age_wealth=4 if age>30 & inc > 10 /* conditions can also be combined with "or" */ gen young=0 replace young=1 if age_wealth==1 | age_wealth==2 Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 32 / 37
  • 33. Basic data management Recoding, dropping variables Recoding variables /* recode happy into sad */ recode happy (1=3) (3=1), gen(sad) Deleting variables drop region // delete region keep age-inc // keep age, educ, sex, and inc Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 33 / 37
  • 34. Basic data management Exercise 4: Manipulating variables 1 Use the dataset, gss.dta 2 Generate a new variable, age2 equal to age squared 3 Generate a new “high income” variable that will take on a value of “1” if a person has an income value greater than “15” and “0” otherwise 4 Generate a new divorced/separated dummy variable that will take on a value of “1” if a person is either divorced or separated and “0” otherwise Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 34 / 37
  • 35. Wrap-up Topic 1 Introduction 2 Getting data into Stata 3 Statistics and graphs 4 Basic data management 5 Wrap-up Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 35 / 37
  • 36. Wrap-up Help us make this workshop better! Please take a moment to fill out a very short feedback form These workshops exist for you – tell us what you need! http://tinyurl.com/6h3cxnz Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 36 / 37
  • 37. Wrap-up Additional resources IQSS workshops: http://projects.iq.harvard.edu/rtc/filter_by/workshops IQSS statistical consulting: http://rtc.iq.harvard.edu The RCE Research Computing Enviroment (RCE) service available to Harvard & MIT users www.iq.harvard.edu/research_computing Wonderful resource for organizing data, running analyses efficiently Creates a centralized place to store data and run analysis Supplies persistent desktop environment accessible from any computer with an internet connection Ista Zahn (IQSS) Introduction to Stata Friday February 8, 2013 37 / 37