

Selectionvector = 1,selectionvectorm, selectionvectoru
MERGE STATA HOW TO
hhper.do - an example of how to match/merge files in stata. In UNIX, issuing the following command (substituting filename with the first name of the file to be submitted - in this case hhper) stata -b do filename & Stata commands are case sensitive on some platforms. *Delete colums with duplicated id values */ selectionvectorm = J(1,cols(fm),1) This will cause STATA to be opened, and the program will be run. *Assembles merged matrix*/ f = idvalues,fm,fu //"idvalues column is added" and the now conform matricies joined. Result: Two matrices of same dimension which can be row-joined.*/ /*Merge Matrix*/įm = J(idvaluesMAX,cols(m).) //matrix collecting expanded mat.įu = J(idvaluesMAX,cols(u).) //matrix collecting expanded mat. – This is done for both matrices supplied. – If the value does not appear in the supplied matrix, – The following loop goes over all values of the ID var andĬhecks these against those supplied in the first matrix.

*Make vector containing all unique values of the ID var*/ idvalues = uniqrows(m\u) U = st_matrix(matu) // a using matrix - hence matu. *Import Stata matricies into Mata*/ m = st_matrix(matm) //Similar to "merge" there is one merging matrix and *Define mata function and it’s arguments*/ mata drop matamatrixmerge()įunction matamatrixmerge(string scalar matm, string scalar matu,string scalar columwithID1, string scalar columwithID2) * each row needs to be uniquely idenfied. * – The matricies are merge using a single id-variable column. * – the function expands the two matrices supplied (mergin/using matrix) columnwidthID1/2: the column number of the column containing the id values for both matm & matu.This function generates the “final” matrix if it is supplied with: mata: matamatrixmerge("matm","matu","columwithID1","columwithID2") Next is the argument using this tells Stata that we are done listing the id variables, and that what follows are the dataset (s) to be merged. Directly after the merge command is the name of the variable (or variables) that serve id variables, in this case id.
MERGE STATA CODE
The code below is for a function, which does this using “Mata”. The first line of syntax below merges the data. I want to merge “matm” and “matu” into a matrix “final” using column 1 as id variable.

– It took me quite a while to get this to work, and any feedback would be very welcome. In this Stata-list post, the suggestion is to either do this using Stata’s command or to import the matrix into Mata in loop over all elements. I estimated svy: proportions over a number of countries for a number of variables and wanted to have one data set containing all results) Occasionally I find myself in the need to combine to matrices of different dimensions from Stata.
