0% found this document useful (0 votes)
37 views

Presentation 3 - Combining Datasets

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Presentation 3 - Combining Datasets

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

EASTERN AFRICA STATISTICAL TRAINING CENTRE

(EASTC)

BACHELOR DEGREE IN OFFICIAL STATISTICS


DATA ANALYSIS WITH STATA

TRAINING MANUAL
Physical Address
By
The Eastern Africa Statistical Training Centre
Edwin Magoti P. O. Box 35103,
Changanyikeni - Dar Es Salaam

Contacts

(+255) 766151460
(+255) 737829252
[email protected]
[email protected]
www.eastc.ac.tz
Magoti, ET - EASTC - 2023
Merging and Appending Data Set

Magoti, ET - EASTC - 2023 3


Presentation Outline
• Combining data
ü append function
ü merge function
ü mmerge function

Magoti, ET - EASTC - 2023 4


Merging and Appending Data Set
• When working with data, it is a practice that
sometimes one may need to combine different
datasets/files to form a sing dataset.
• STATA provides room for achieving this.
• There are two main ways that can be used in
combining data:
oAppending
oMerging
• Each of these has its own functionality and
usefulness as discussed.

Magoti, ET - EASTC - 2023 5


Appending Data Set
• The command append used in combining two datasets
by placing the second data set at the end (next row just
after the last observations)of the master data set.
• The Primary objective of appending is to add more
observations.
o Therefore, datasets/files should have same
(number) variables, and preferably, same
variable names.
• However, it is also possible to append data having
unequal number number of variables.
• In this case, missing values will be introduced whenever
there is a mismatch. I

Magoti, ET - EASTC - 2023 6


Appending Data Set
• General Syntax:
append using ”filename.dta”, [Options]
append using “filename.dta”, gen(Newvariable)

• This is however applicable when the correct working


directory is set.
• If the working directory is not set, you will need to
specify the entire file path.
• Example:
append using “/Users/mac/Library/Mobile Documents/EASTC/filename.dta”

Magoti, ET - EASTC - 2023 7


Appending Data Set
• General Syntax:
append using ”filename.dta”, [Options]
append using “filename.dta”, gen(Newvariable)

Magoti, ET - EASTC - 2023 8


Appending • Illustration on how appending of data is
Data Set done

Magoti, ET - EASTC - 2023 9


Merging Data Set

• Formally; the function merge used in combining two


datasets having same observations but different
variables.
ü The Primary objective is to add more Variables.
• Each dataset must have a variable (variables) that
“uniquely identifies” observations in the respective
datasets.
• The variable(s) that unique identifies observations in
the given datasets are used as merging/matching
variable(s)

Magoti, ET - EASTC - 2023 10


Merging Data Set

• Merging can be:


o One-to-ton key identifier: Both master and using
data have only one unique observation in the key
variable
o One-to-many key identifier: One observation in the
master data and at least two matching
observations Using data.
o Many-to-one key identifier: More than one
observation in the master data and only one
matching observations Using data.
o Many-to-many key identifier: Both Using and One
observation in the master data and at least two
matching observations Using data.

Magoti, ET - EASTC - 2023 11


Merging Data Set

General Syntax:
o merge 1:1 UniqueVarName using Dataset2.dta
o merge m:1 UniqueVarName using Dataset2.dta
o merge 1:m UniqueVarName using Dataset2.dta
o merge m:m UniqueVarName using Dataset2.dta

Magoti, ET - EASTC - 2023 12


Merging Data
Set

Illustration on merging
datasets

Magoti, ET - EASTC - 2023 13


Appending and
Merging Data
Set

Appending and Merging


can be accessed from the
drop-down menue.
Go to: Data --> Combine
datasets; then select either
merge two datasets or
append datasets

Magoti, ET - EASTC - 2023 14


Appending and
Merging Data
Set

If you wish to merge data,


then select Merge two
datasets proceed
accordingly.

Magoti, ET - EASTC - 2023 15


Appending and
Merging Data
Set

If you wish to append data,


then select Append
datasets proceed
accordingly.

Magoti, ET - EASTC - 2023 16


Combining dataset
mmerge is an extension of
merge that makes matched
Another very useful function in merging safe. It requires users
combining datasets is mmerge. to specify the type of match to
be performed; mmerge verifies
that the requirements hold.

General Syntax:
• Basic syntax
• mmerge match-variable(s) using
It also makes merging easy, filename
• The syntax goes with serval options,
these can be accessed using the help
command in STATA, (help mmerge).

Magoti, ET - EASTC - 2023 17

You might also like