Bookmark and Share

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


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

Re: st: Re: Merge in Mata


From   Pedro Nakashima <[email protected]>
To   [email protected]
Subject   Re: st: Re: Merge in Mata
Date   Fri, 28 Dec 2012 11:51:25 -0200

I was thinking of something like

clear mata
mata
A= (1\4\5\7)
B= (1,13\5,27)
Cdesired = (1,13\4,.\5,27\7,.)
C= J(rowmax((rows(A),rows(B))),rowmax((cols(A),cols(B))),.)
A
B
C
Cdesired
col = A\B[.,1]
col = mm_collapse(col,1,col)[.,1]
col
C[.,1] = col
C
for (i=1; i<=rows(B); i++) {
pos = mm_posof(C[.,1],B[i,1])
pos
C[pos,2] = B[i,1]
}
C
end

where A and B came from a byable Stata program (with syntax newvar),
which has dealed with Panel data

An then I want the C back to Stata data (with observations matched)

The second column would be the new variable generated by the Stata program

2012/12/28 Gordon Hughes <[email protected]>:
> 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
> [email protected]
>
> *
> *   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/
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index