Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: Merge in Mata

From   Gordon Hughes <>
Subject   st: Re: Merge in Mata
Date   Fri, 28 Dec 2012 11:37:47 +0000

If I understand the original posting correctly, what you are trying to carry out is a true match merge [using the terminology of the Stata manual entry for -merge-] with general values of the vector and matrix. Assuming that is correct, then Mata does not provide any direct way of doing this. Then, the question is how general you want to be.

A sketch of some special purpose code for vector v and matrix M would be as follows:

1. Use -sort()- to row-sort both M and a matrix V consisting of v augmented by a column of original row numbers.

2. Loop through V & M to match rows identified by the element in the first column of each, thus creating V_M.

3. Decide how to treat non-matched rows in V & M and cases with multiple matches.

4. Finally, re-sort V_M on the row indices of v in column 2 and drop this column.

None of this is difficult - provided that you are clear in dealing with non-matches and other special cases - and it can be easily implemented as a Mata function.

An alternative would be to write v & M to temporary Stata datasets and use -merge- to join them. This will certainly be more expensive. It may or may not be simpler from your perspective, depending upon how you are using Mata - e.g. you would not want to do this when programming a stand-alone Mata function or procedure.

Gordon Hughes

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index