Download presentation
Presentation is loading. Please wait.
1
Introduction Introduction to Stata 2016
2
And how will we do this? I introduce and demonstrate a topic and a set of commands You try the same commands on your computer I (sometimes) give you small assignments to complete You report your experience and we discuss any problems that occured New softwares can only be learnd by ”learning by doing”
3
What do we Find? A result window (Stata speaks to us)
A review window (shows executed commands) A variable window (shows the variables in the data set) and A command interface (where we tell Stata what to do)
5
Using the Do-files The do-files in Stata is a text file with commands that can be run directly from Stata The do-files stores your commands By using do-files you always have a good documentation of your work such as codings etc. It also makes it easy for you to repeat or modify your analyses By using do-files you never have to make any changes in your data.
6
Data for exercise Download :exercise 1 and 2 to your computer
7
Using the Do-files A do-file should initially look something like this: .clear .set more off .use c:/exercise2.dta
8
Using the Do-files .clear – clears the data in memory . Otherwise no new data can be opened. .set more off – Tell Stata to execute all commands inspite of screen size Dots before any commands are standard in most books on Stata (I use it as well so you get used to it) More over, Stata is sensitive for capital- and lower case letters.
9
Using the help command By typing .help command in the ci many problems can be solved. Try any of the following: .help desc .help lab .help list
10
renaming the data Changing variable names Labeling variables
The rename command in the ci: .rename old_varname new_varname Labeling variables .label variable varname [”label”] e.g. label variable sex ”Gender”
11
Examining your data II Some helpful commands to examine your data more carefully .tabulate/tabulate1 .tabstat .summarize .list .browse .order .sort/gsort .inspect .describe .codebook
12
Examining your data II Let us now go through each command and see what it can do for us using the ” exercise1.dta”. .tabulate (tab) – tabulates our variables. The command requires a variable list .tab vars .tab v39 If you want to tab several variables: .tab1 v39 v35x v40
13
Examining your data II .summarize (sum) – summarize our variables.
(If no variables in varlist=all variables). .sum .sum vars .sum vars, d (d=detailed – gives more information such as median values etc.) For example: .sum v39 .sum v39, d (d=detailed) .sum v39 v35x v40
14
Examining your data II .list – list variables. The command shows the values for a specific observation on a certain variable or all variables .list shows the values for a specific observation .list vars shows the values for a specific observation on a specific variable For example: .list v39 .list v39 v35x v40 Type set more off for long outputs
15
Examining your data II .order – order the variables. The command hence requires a variable list .order vars For example: .order v39 v35x v40
16
Examining your data II .sort –arranges the observations of the current data into ascending order based on the values of the variables in varlist. (.sort vars) For example: .sort v39 .sort v39 v35x v40
17
Examining your data II Even better is .gsort [-|+] that arranges the observations into ascending or decending order such as: .gsort –var1 .gsort +var1 .gsort +var1 var2 var3 etc
18
Examining your data II .inspect –Display simple summary of data's attributes. It is a bit more detailed compared to sum or tab and is useful for numerical vars. For example: .inspect .inspect vars . inspect v39 v35x v40
19
Examining your data II .describe – Describe data in memory or in file
For example: .describe .describe vars .describe v39 v35x v40
20
Examining your data II .codebook – describe data contents and the output is often useful for printing. It also gives information of variable characteristics such as numeric or string For example: .codebook .codebook vars .codebook v39 v35x v40
21
. command varlist, [options]
Stata options Stata’s general grammar is very straight forward and most commands can be executed with different options. . command varlist, [options]
22
Stata options Stata’s general grammar is very straight forward and most commands can be executed with different options. To see which options that are available – type: .help command
23
Stata options . sum varlist, [d]
Let’s try the some of the commands we learnt with their options (we have already tried one) . sum varlist, [d] [d] is here our option
24
Useful options for tabulate :
Stata options Let’s try the some of the commands we learnt with their options (we have already tried one) Useful options for tabulate : , sort , nolabel , missing . tab varlist, []
25
Stata options .tab varlist, [] .tab v39, sort .tab v40, nolabel
For example, type: v39 v35x v40 .tab v39, sort .tab v40, nolabel .tab1 v39 v40 , missing (underscore means abbreviations for the ci)
26
Stata options Let’s continue with the options
Stata allows for a wide range of different options or pre-post commands that can be used with the main commands. . [by varname] command varlist [in] [if], [options]
27
Stata options Let’s continue with the options and introduce the if-statements An if-statment means that the command only is executed for those observations who fulfill the condition you specify . command varlist [if], [options]
28
Stata options Try the following statements:
If-statments should be specified before the comma and can be combined with other options such as: .command varlist if var1==x, [options] Try the following statements: . tab u39 if v35x==1 . tab u39 if v35x==1, m (shows var values with missing included) Here we are simply tabulating the values of satisfaction with life for all men
29
Stata options What’s the level of life satisfaction among young and old people? .tab u39 if v42x <1973 or .tab u39 if v42x >1970
30
Stata options – common Stata operators
31
Stata options – common Stata operators
Some noteworthy operators: | or == equal to (as comprison) != not equal to (as comprison) ^ exponent (eg. 2^2=4)
32
Stata options – common Stata operators
Lets try the OR operator .tab varlist if varX==Z | varX==Y For example: tab w39 if v42x>1970 & v35x==1 & v40==1 | v40==4
33
Stata options – common Stata operators
Now we have introduced the IF-statement. . [by varname] command varlist [in] [if], [options] Let’s look at the IN-statement. . command varlist [in], [options]
34
Stata options – common Stata operators
Specifics for the in qualifier: f the first observation in the data set l the last observation in the data set Such as: .command varlist in f/l
35
Stata options – common Stata operators
Examples of in-statements: command meaning list in 1/10 list the first ten observations list in f/10 list the first ten observations list in 5/15 list observation nr 5 to 15 list in 5/l list from observation 5 to the end list in -5/l list the last five observations list in 10 list only observation nr ten
36
Stata options – common Stata operators
If you want to keep or drop variables Example: .drop/keep var3 – var5 Or labels .label drop labelname Or observations .drop in 45/65
37
Stata options – common Stata operators
Lets say you (for some reason) want to find the level of education among the first 20 respondents that are very satisfied with their lifes .gsort +v39 .list v40 v39 in 1/20 Or the last 20 respondents .list v40 v39 in -20/l
38
Stata options And finally, let’s check out the by option.
. [by varname] command varlist [in] [if], [options] With the by command you can receive the values of variable x for every value of variable x, such as: .by x: tab z However, the by command only works for sorted data… There are always several ways to do things in Stata
39
Stata options Solutions (There are always several ways to do things in Stata): 1, sort the variabel and then use the by command (the long way) .sort x .by x: tab z 2, sort directly after the by command such as: .by x, sort: tab z 3, or even better, use the bysort command .bysort x: tab z
40
Stata options Let’s try the bysort function For example Etc.
.bysort v40: tab v39 .bysort v40: sum v39 Etc.
41
Conclusively – what have we learnt
Working with do-files More on data examination Creating simple univariate tables Sorting your observations Re-ordering your variables Stata logical operators Using command qualifiers, if, in and by statements
42
Exploring data Before we move over to data management (which is the next subject), let’s practice what we’ve learnt so far… with a new (and more interesting) data set. so….. Clear Stata Create a new do-file. Type in neccessary set-commands Load the data file ” exercise2.dta” (Make all this in the do-file)
43
.clear .set more off .use exercise2.dta This data is based on QoG with country/years as units of analysis.
44
Examining your data II Take a look at the data, explore the variables
use the commands below combined with the if/in/by options .tabulate/tabulate1 .summarize .list .browse .order .sort/gsort .inspect .describe .codebook
45
Question 1 Which are the top ten countries_year observations in terms of having most GDP per capita?
46
Solution(s) Or .clear .set more off .tab cname_year in 1/10
use "C:\exercise2.dta", clear gsort -mad_gdppc list cname_year in 1/10 Or .tab cname_year in 1/10
47
Question 2 What is the mean value of GDP per capita among countries that have a religious Fractionalization below or above the 25:th and 75:th percentile values?
48
Solution: sum al_religion, d al_religion <= 25:th perc. = .232
.sum mad_gdppc if al_religion<=3.02 .sum mad_gdppc if al_religion>=4.18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.