Merge Stata


Occasionally I find myself in the need to combine to matrices of different dimensions from Stata.

You merge when you want to add more variables to an existing dataset (type help merge in the command window for more details) What you need: Both files should be in Stata format Both files should have at least one variable in common (id) Step1. How to Use the STATA merge and reshape commands Most of the projects done in 17.871, and in fact most interesting research, require combining data sets. This handout reviews using the most valuable command for managing multiple data sets, the merge command. In addition, we are often interested in combining multiple. But since merge is a bit trickier we will focus on that command in this guide. Different ways of merging datasets¶ With the merge command we can do three (or really, two) different types of merges. Easiest is the 1:1 merge. It means that we have the same observations in both datasets.

(E.g. I estimated svy: proportions over a number of countries for a number of variables and wanted to have one data set containing all results)

In this Stata-list post, the suggestion is to either do this using Stata’s [merge] command or to import the matrix into Mata in loop over all elements. – It took me quite a while to get this to work, and any feedback would be very welcome.


I want to merge “matm” and “matu” into a matrix “final” using column 1 as id variable.

The code below is for a function, which does this using “Mata”.

Merge Stata M:1

This function generates the “final” matrix if it is supplied with:

Merge Stata Command

  • matm: a merging matrix
  • matu: a using matrix
  • columnwidthID1/2: the column number of the column containing the id values for both matm & matu.

State code:

*Define mata function to merge

* – the function expands the two matrices supplied (mergin/using matrix)
* to achieve conformability.
* – The matricies are merge using a single id-variable column.
* [ACHTUNG:] each row needs to be uniquely idenfied.
* [Output:] a merged Stata matrix called “final”.

How To Merge Datasets In Stata

/*Define mata function and it’s arguments*/

Merge Stata

Merge Stata

/*Import Stata matricies into Mata*/

/*Make vector containing all unique values of the ID var*/

Merge Stata One To Many

– The following loop goes over all values of the ID var and
checks these against those supplied in the first matrix.
– If the value does not appear in the supplied matrix,
a row of missing values is added.
– This is done for both matrices supplied.
Result: Two matrices of same dimension which can be row-joined.*/

/*Assembles merged matrix*/

/*Delete colums with duplicated id values */

/*Export final matrix from Mata to Stata*/

/*Save the function in current folder.*/