Regional Course on SDGs Indicators: Measuring decent work
using microdata from labour force surveys
STATA basics - Getting started and Managing dataChiba (Japan)
30 September – 4 October 2019

Objectives
This is an introduction to the statistical software STATA aiming at:
 Preparing the participants in STATA basics (interphase andcommands) for the next days’ sessions.
 Doing some preliminary inspection and data manipulation using theLabour Force Survey (LFS) of Nepal 2017.

ilostat.ilo.org

2

What is Stata?
 It is a multi-purpose statistical package to explore summarize andanalyze information organized in datasets.
 Its first version was officially released in January 1985. The lastversion (Stata V.15) in 2015.
 Stata is widely used in social science research (especiallyeconomics, political science, epidemiology and medical science)and the most used statistical software at the ITC-ILO campus.
 Other statistical software: SPSS, SAS, R, etc.

ilostat.ilo.org

3

Stata 13 screen

ilostat.ilo.org

4Stata 13 screen

ilostat.ilo.org

5

The Stata’s toolbar

1. Open:Opens a new data file (use)2. Save:
Saves the current data file (save)
3. Print:
Prints the content of the results window.
4. Log:Begin/close/suspend/resume a log file.
5. New Viewer:
Opens a new viewer window to obtain help.
6. Graph:
Bring back the graph window in front.
7. New Do-file Editor: Opens a new instance of the do-file editor (doedit).
8. Data Editor:
Opens the data editor window (edit).
9. Data Browser:
Opens the data browser (browse).
10. Variable manager: Manipulate variables.
11. More:Continues when paused in long output.
12. Break:
Allows canceling current running calculations.

ilostat.ilo.org

6Ways of working with Stata
1. Interactively: click through the menu/toolbar or typing directly thecommands in the command window.
2. Batch mode: type up a list of commands in a “do-file” and thenexecute the file.
Using the batch mode (do-files) is the best way to work, because it
allows us:
a.
b.
c.
d.
e.
f.

To save our work and keep track of it.
To repeat (copy/paste) commands at convenience.
To suspend/stop our work
To find and fix errors or mistakes
To share our code with colleagues
To better handle a long list of commands (usually the case!)

ilostat.ilo.org

7

The batch mode: «do-file»
1. To open a do-file, click on the «do-file» editor in the toolbar menu
or type the command doedit on the command window.
2. Write down the commands in the do-file window.
3. Execute the entire do-file by clicking on the last menu button(Execute (do))
4. If you want to execute specific lines: select them and click theexecuted button.
5. To add comments (green-colored):

6. Save the do-file by clicking on the “save” iconilostat.ilo.org

8

Importing and saving data
Importing data in Stata
Stata’s dataset format: “.dta” extension• Interactively: click on the open icon
• Command: use “my_file.dta”, clear
Data in other format:• Command: insheet using filename (for formats: .csv .txt .xls)••

Interactively: click on “File”  “Import”
Using StatTransfer software or other tools that could helps us tosave our data in .dta format.

Saving data in Stata•

Command: save “my_file”, replace (saves as my_file;replace is necessary if a file with the same name already exists inthe directory and wants to be replaced).

ilostat.ilo.org

9Data structure•

A dataset is a collection of separate sets of information usuallycalled variables (commonly arranged by columns). (The Cambridge
dictionary).

•

One variable is a set of information containing observed, measured
or reported characteristics for one or several cases/observations.

•Typical grid structure-- Each row represents the unit of observation (an individual, a firm, aregion)-- Each column represents the values that variable takes for eachobservation (age, sex, educational attainment, etc.).

ilostat.ilo.org

10Data structure
Column: variable (e.g. sex, age, etc.)

Row: unit ofobservation(here, aperson)

ilostat.ilo.org

11

Variables
In Stata variables can be recorded either as:••

Numeric: may contain only numbers (e.g. age, wage), orString: may contain letters or numbers referring to categories (e.g.
education)

Values of string variables are included in double quotes:
generate men=1 if sex==“male”
Whereas values of numeric variables not
generate young=1 if age<=25Variables may contain missing values:• Missing values in string variables  empty double quotes: “ ”• Missing values in numeric variables  a dot: .
ilostat.ilo.org

12

Variables
Some information when naming variables:
••

•
•
•
•

Variable names can be up to 32 written characters long
Nonetheless – for displaying purposes – a max of 10 characters is
recommended to name the variables. (otherwise it will be shown as
high_educa~n; shorten after the 10th character)
The name can contain lower and uppercase letters, numbers andthe underscore “_” character.
Given that Stata is case sensitive (unlike SAS for instance), it isbetter to use lowercase. (age ≠ Age)
The name cannot contain blank spaces or special characters (% ! ?
, : ; .)
It has to start with a letter or underscore (not a number)

ilostat.ilo.org

13

Writing commands•

Stata is case sensitive: all commands are lowercase.

• Standard structure for commands:
command varlist if/in, options•Commands can be abbreviated up to their underlined option; thus,
command can be abbreviated as:

 comman comma comm com But not!: co•

If you want to check the syntax to use for each command
help command_nameilostat.ilo.org

14

Help window: help rename

15

ilostat.ilo.org

Logical and relational operators
==
!=>
>=
<<=&|

ilostat.ilo.org

equal tonot equal togreater thangreater or equal toless thanless or equal to(logical) and(logical) or

16

Examining the data: some commands
usecommandbrowseView raw data

describe

Produces a summary of the dataset

codebook var1 var2

Examines the variables names, labels and datasummarize

Calculates and displays a variety of univariate
summary statistics

tabulate var1

Univariate frequency tablekeep var1 var2

Keeps in memory only the mentioned variables
or observations
keep and drop commands are not reversibleorder var1 var2

Relocates var1 and var2 to the beginning of thedataset in the order in which the variables are
specifiedcount var1 if exp

Counts the number of observation of variable if
expression is true
17

ilostat.ilo.org

Organizing the data: generate and replacecommanduse

generate newvar=exp [if][in]Creates a new variablereplace oldvar=exp [if][in]Replace contents of existing variable

Example:Generate the categorical variable “working-age population” that takes
the value 1 if the person’s age is greater or equal to 15, and 0
otherwise; name it “wap”. (age is stored in variable b02)
generate wap =.
replace wap=0 if b02<15
replace wap=1 if b02>=15 & b02!=.

ilostat.ilo.org

18Organizing the data: labelling•

Labelling variables with descriptive names is useful and helps tobetter follow their meaning.
• Labelling values of categorical variables ensures that the real-world
meanings of the encodings are not forgotten.
 These points are crucial when sharing data with others, including
yourself in the future.
usecommandlabel variable var1 “my_var_name”

1. label define set_of_val_label 0 “…” 1 “…” 2 “…”
2. label value var1 set_of_val_label

Labels the variable
1. Defines the set of valuelabels to each category.
2. Attaches the value labels
previously defined to thevalues of var1

ilostat.ilo.org

19

Let’s move to Stata !
1. Launch your Stata version by clicking on the Stata icon on thedesktop.
2. Open the do-file by pressing the do-file editor

3. Search in the Introductory_Stata folder and click on:“Introductory_Session.do”
4. .. Let’s move to Stata

ilostat.ilo.org

20Some Stata help can be found on:••••Stata website (www.stata.com)
Help online
Manuals:
Acock, A Gentle Introduction to Stata, 3rd Edition, Stata Press, 2010
Baum, An Introduction to Modern Econometrics Using Stata, 2006
Cameron and Trivedi, Microeconometrics Using Stata, revised
edition, Stata Press 2010
University of California resource Centre:
www.ats.ucla.edu/stat/stata

ilostat.ilo.org

21