Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

st: Re: Merge in Mata


From   Gordon Hughes <G.A.Hughes@ed.ac.uk>
To   statalist@hsphsun2.harvard.edu
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
g.a.hughes@ed.ac.uk

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index