mata mata drop varmatch() real vector function varmatch(from, to) { matches = J(1,rows(to),0) for (i=1; i<=rows(from); i++) { matchi = (from[i,2]:==to[.,2])' matches = matches + matchi } return(matches) } //varmatch(A2c, A4c)' //select(A4c, varmatch(A2c,A4c)') //select(A4c, 1:-varmatch(A2c,A4c)') end mata mata drop varmatchpos() real vector function varmatchpos(from, to) { matches = J(1,rows(from),0) for (i=1; i<=rows(to); i++) { matchi = (from[.,2]:==to[i,2])':*i matches = matches + matchi } return(matches) } //S2 = J(1,cols(A4),.) //S2[1,varmatchpos(A2c, A4c)] = A2 //S2 //S3[1,varmatchpos(A3c, A4c)] = A3 //S1[1,varmatchpos(A1c, A4c)] = A1 //S3 //S1 end