Stata: Continent variable based on Country variable (ISO code [ISO3166- numeric])

Standard

I recently wanted to examine migrants’ labour market situation  by their continent of birth , however datasets like the EU-Labour Forces Survey or EU-SILC or the UK-LFS, don’t provide a continent variable.

Below you can find the Stata code to generate a continent variable (e.g. indicating Antarctica as continent of origin) based on a variable containing country information (in ISO 3-digit format, e.g. 404 = “Kenya”).

*Generate empty string variable
gen continent = ""

*Replace empty observations with Continent string given a country variable.<\code>
replace continent = "Not defined" if(country_origin==-9 & continent=="")
replace continent = "Not defined" if(country_origin==-8 & continent=="")
replace continent = "AS" if(country_origin==4 & continent=="")
replace continent = "EU" if(country_origin==8 & continent=="")
replace continent = "ANT" if(country_origin==10 & continent=="")
replace continent = "AF" if(country_origin==12 & continent=="")
replace continent = "OC" if(country_origin==16 & continent=="")
replace continent = "EU" if(country_origin==20 & continent=="")
replace continent = "AF" if(country_origin==24 & continent=="")
replace continent = "CAR" if(country_origin==28 & continent=="")
replace continent = "AS" if(country_origin==31 & continent=="")
replace continent = "SA" if(country_origin==32 & continent=="")
replace continent = "OC" if(country_origin==36 & continent=="")
replace continent = "EU" if(country_origin==40 & continent=="")
replace continent = "CAR" if(country_origin==44 & continent=="")
replace continent = "AS" if(country_origin==48 & continent=="")
replace continent = "AS" if(country_origin==50 & continent=="")
replace continent = "AS" if(country_origin==51 & continent=="")
replace continent = "CAR" if(country_origin==52 & continent=="")
replace continent = "EU" if(country_origin==56 & continent=="")
replace continent = "CAR" if(country_origin==60 & continent=="")
replace continent = "AS" if(country_origin==64 & continent=="")
replace continent = "SA" if(country_origin==68 & continent=="")
replace continent = "EU" if(country_origin==70 & continent=="")
replace continent = "AF" if(country_origin==72 & continent=="")
replace continent = "ANT" if(country_origin==74 & continent=="")
replace continent = "SA" if(country_origin==76 & continent=="")
replace continent = "MA" if(country_origin==84 & continent=="")
replace continent = "AS" if(country_origin==86 & continent=="")
replace continent = "OC" if(country_origin==90 & continent=="")
replace continent = "CAR" if(country_origin==92 & continent=="")
replace continent = "AS" if(country_origin==96 & continent=="")
replace continent = "EU" if(country_origin==100 & continent=="")
replace continent = "AS" if(country_origin==104 & continent=="")
replace continent = "AF" if(country_origin==108 & continent=="")
replace continent = "EU" if(country_origin==112 & continent=="")
replace continent = "AS" if(country_origin==116 & continent=="")
replace continent = "AF" if(country_origin==120 & continent=="")
replace continent = "AN" if(country_origin==124 & continent=="")
replace continent = "AF" if(country_origin==132 & continent=="")
replace continent = "CAR" if(country_origin==136 & continent=="")
replace continent = "AF" if(country_origin==140 & continent=="")
replace continent = "AS" if(country_origin==144 & continent=="")
replace continent = "AF" if(country_origin==148 & continent=="")
replace continent = "SA" if(country_origin==152 & continent=="")
replace continent = "AS" if(country_origin==156 & continent=="")
replace continent = "AS" if(country_origin==158 & continent=="")
replace continent = "AS" if(country_origin==162 & continent=="")
replace continent = "AS" if(country_origin==166 & continent=="")
replace continent = "SA" if(country_origin==170 & continent=="")
replace continent = "AF" if(country_origin==174 & continent=="")
replace continent = "AF" if(country_origin==175 & continent=="")
replace continent = "AF" if(country_origin==178 & continent=="")
replace continent = "AF" if(country_origin==180 & continent=="")
replace continent = "OC" if(country_origin==184 & continent=="")
replace continent = "MA" if(country_origin==188 & continent=="")
replace continent = "EU" if(country_origin==191 & continent=="")
replace continent = "CAR" if(country_origin==192 & continent=="")
replace continent = "EU" if(country_origin==203 & continent=="")
replace continent = "AF" if(country_origin==204 & continent=="")
replace continent = "EU" if(country_origin==208 & continent=="")
replace continent = "CAR" if(country_origin==212 & continent=="")
replace continent = "CAR" if(country_origin==214 & continent=="")
replace continent = "SA" if(country_origin==218 & continent=="")
replace continent = "CAR" if(country_origin==222 & continent=="")
replace continent = "AF" if(country_origin==226 & continent=="")
replace continent = "AF" if(country_origin==231 & continent=="")
replace continent = "AF" if(country_origin==232 & continent=="")
replace continent = "EU" if(country_origin==233 & continent=="")
replace continent = "EU" if(country_origin==234 & continent=="")
replace continent = "SA" if(country_origin==238 & continent=="")
replace continent = "ANT" if(country_origin==239 & continent=="")
replace continent = "OC" if(country_origin==242 & continent=="")
replace continent = "EU" if(country_origin==246 & continent=="")
replace continent = "EU" if(country_origin==248 & continent=="")
replace continent = "EU" if(country_origin==250 & continent=="")
replace continent = "SA" if(country_origin==254 & continent=="")
replace continent = "OC" if(country_origin==258 & continent=="")
replace continent = "ANT" if(country_origin==260 & continent=="")
replace continent = "AF" if(country_origin==262 & continent=="")
replace continent = "AF" if(country_origin==266 & continent=="")
replace continent = "AS" if(country_origin==268 & continent=="")
replace continent = "AF" if(country_origin==270 & continent=="")
replace continent = "AS" if(country_origin==275 & continent=="")
replace continent = "EU" if(country_origin==276 & continent=="")
replace continent = "AF" if(country_origin==288 & continent=="")
replace continent = "EU" if(country_origin==292 & continent=="")
replace continent = "OC" if(country_origin==296 & continent=="")
replace continent = "EU" if(country_origin==300 & continent=="")
replace continent = "AN" if(country_origin==304 & continent=="")
replace continent = "CAR" if(country_origin==308 & continent=="")
replace continent = "CAR" if(country_origin==312 & continent=="")
replace continent = "OC" if(country_origin==316 & continent=="")
replace continent = "MA" if(country_origin==320 & continent=="")
replace continent = "AF" if(country_origin==324 & continent=="")
replace continent = "SA" if(country_origin==328 & continent=="")
replace continent = "CAR" if(country_origin==332 & continent=="")
replace continent = "ANT" if(country_origin==334 & continent=="")
replace continent = "EU" if(country_origin==336 & continent=="")
replace continent = "MA" if(country_origin==340 & continent=="")
replace continent = "AS" if(country_origin==344 & continent=="")
replace continent = "EU" if(country_origin==348 & continent=="")
replace continent = "EU" if(country_origin==352 & continent=="")
replace continent = "AS" if(country_origin==356 & continent=="")
replace continent = "AS" if(country_origin==360 & continent=="")
replace continent = "AS" if(country_origin==364 & continent=="")
replace continent = "AS" if(country_origin==368 & continent=="")
replace continent = "EU" if(country_origin==372 & continent=="")
replace continent = "AS" if(country_origin==376 & continent=="")
replace continent = "EU" if(country_origin==380 & continent=="")
replace continent = "AF" if(country_origin==384 & continent=="")
replace continent = "CAR" if(country_origin==388 & continent=="")
replace continent = "AS" if(country_origin==392 & continent=="")
replace continent = "AS" if(country_origin==398 & continent=="")
replace continent = "AS" if(country_origin==400 & continent=="")
replace continent = "AF" if(country_origin==404 & continent=="")
replace continent = "AS" if(country_origin==408 & continent=="")
replace continent = "AS" if(country_origin==410 & continent=="")
replace continent = "AS" if(country_origin==414 & continent=="")
replace continent = "AS" if(country_origin==417 & continent=="")
replace continent = "AS" if(country_origin==418 & continent=="")
replace continent = "AS" if(country_origin==422 & continent=="")
replace continent = "AF" if(country_origin==426 & continent=="")
replace continent = "EU" if(country_origin==428 & continent=="")
replace continent = "AF" if(country_origin==430 & continent=="")
replace continent = "AF" if(country_origin==434 & continent=="")
replace continent = "EU" if(country_origin==438 & continent=="")
replace continent = "EU" if(country_origin==440 & continent=="")
replace continent = "EU" if(country_origin==442 & continent=="")
replace continent = "AS" if(country_origin==446 & continent=="")
replace continent = "AF" if(country_origin==450 & continent=="")
replace continent = "AF" if(country_origin==454 & continent=="")
replace continent = "AS" if(country_origin==458 & continent=="")
replace continent = "AS" if(country_origin==462 & continent=="")
replace continent = "AF" if(country_origin==466 & continent=="")
replace continent = "EU" if(country_origin==470 & continent=="")
replace continent = "CAR" if(country_origin==474 & continent=="")
replace continent = "AF" if(country_origin==478 & continent=="")
replace continent = "AF" if(country_origin==480 & continent=="")
replace continent = "AN" if(country_origin==484 & continent=="")
replace continent = "EU" if(country_origin==492 & continent=="")
replace continent = "AS" if(country_origin==496 & continent=="")
replace continent = "EU" if(country_origin==498 & continent=="")
replace continent = "EU" if(country_origin==499 & continent=="")
replace continent = "CAR" if(country_origin==500 & continent=="")
replace continent = "AF" if(country_origin==504 & continent=="")
replace continent = "AF" if(country_origin==508 & continent=="")
replace continent = "AS" if(country_origin==512 & continent=="")
replace continent = "AF" if(country_origin==516 & continent=="")
replace continent = "OC" if(country_origin==520 & continent=="")
replace continent = "AS" if(country_origin==524 & continent=="")
replace continent = "EU" if(country_origin==528 & continent=="")
replace continent = "CAR" if(country_origin==530 & continent=="")
replace continent = "CAR" if(country_origin==531 & continent=="")
replace continent = "CAR" if(country_origin==533 & continent=="")
replace continent = "CAR" if(country_origin==534 & continent=="")
replace continent = "CAR" if(country_origin==535 & continent=="")
replace continent = "OC" if(country_origin==540 & continent=="")
replace continent = "OC" if(country_origin==548 & continent=="")
replace continent = "OC" if(country_origin==554 & continent=="")
replace continent = "MA" if(country_origin==558 & continent=="")
replace continent = "AF" if(country_origin==562 & continent=="")
replace continent = "AF" if(country_origin==566 & continent=="")
replace continent = "OC" if(country_origin==570 & continent=="")
replace continent = "OC" if(country_origin==574 & continent=="")
replace continent = "EU" if(country_origin==578 & continent=="")
replace continent = "OC" if(country_origin==580 & continent=="")
replace continent = "OC" if(country_origin==581 & continent=="")
replace continent = "OC" if(country_origin==583 & continent=="")
replace continent = "OC" if(country_origin==584 & continent=="")
replace continent = "OC" if(country_origin==585 & continent=="")
replace continent = "AS" if(country_origin==586 & continent=="")
replace continent = "MA" if(country_origin==591 & continent=="")
replace continent = "OC" if(country_origin==598 & continent=="")
replace continent = "SA" if(country_origin==600 & continent=="")
replace continent = "SA" if(country_origin==604 & continent=="")
replace continent = "AS" if(country_origin==608 & continent=="")
replace continent = "OC" if(country_origin==612 & continent=="")
replace continent = "EU" if(country_origin==616 & continent=="")
replace continent = "EU" if(country_origin==620 & continent=="")
replace continent = "AF" if(country_origin==624 & continent=="")
replace continent = "AS" if(country_origin==626 & continent=="")
replace continent = "CAR" if(country_origin==630 & continent=="")
replace continent = "AS" if(country_origin==634 & continent=="")
replace continent = "AF" if(country_origin==638 & continent=="")
replace continent = "EU" if(country_origin==642 & continent=="")
replace continent = "EU" if(country_origin==643 & continent=="")
replace continent = "AF" if(country_origin==646 & continent=="")
replace continent = "CAR" if(country_origin==652 & continent=="")
replace continent = "AF" if(country_origin==654 & continent=="")
replace continent = "CAR" if(country_origin==659 & continent=="")
replace continent = "CAR" if(country_origin==660 & continent=="")
replace continent = "CAR" if(country_origin==662 & continent=="")
replace continent = "CAR" if(country_origin==663 & continent=="")
replace continent = "CAR" if(country_origin==666 & continent=="")
replace continent = "CAR" if(country_origin==670 & continent=="")
replace continent = "EU" if(country_origin==674 & continent=="")
replace continent = "AF" if(country_origin==678 & continent=="")
replace continent = "AS" if(country_origin==682 & continent=="")
replace continent = "AF" if(country_origin==686 & continent=="")
replace continent = "EU" if(country_origin==688 & continent=="")
replace continent = "AF" if(country_origin==690 & continent=="")
replace continent = "AF" if(country_origin==694 & continent=="")
replace continent = "AS" if(country_origin==702 & continent=="")
replace continent = "EU" if(country_origin==703 & continent=="")
replace continent = "AS" if(country_origin==704 & continent=="")
replace continent = "EU" if(country_origin==705 & continent=="")
replace continent = "AF" if(country_origin==706 & continent=="")
replace continent = "AF" if(country_origin==710 & continent=="")
replace continent = "AF" if(country_origin==716 & continent=="")
replace continent = "AF" if(country_origin==728 & continent=="")
replace continent = "AF" if(country_origin==729 & continent=="")
replace continent = "AF" if(country_origin==732 & continent=="")
replace continent = "AF" if(country_origin==736 & continent=="")
replace continent = "SA" if(country_origin==740 & continent=="")
replace continent = "EU" if(country_origin==744 & continent=="")
replace continent = "AF" if(country_origin==748 & continent=="")
replace continent = "EU" if(country_origin==752 & continent=="")
replace continent = "EU" if(country_origin==756 & continent=="")
replace continent = "AS" if(country_origin==760 & continent=="")
replace continent = "AS" if(country_origin==762 & continent=="")
replace continent = "AS" if(country_origin==764 & continent=="")
replace continent = "AF" if(country_origin==768 & continent=="")
replace continent = "OC" if(country_origin==772 & continent=="")
replace continent = "OC" if(country_origin==776 & continent=="")
replace continent = "CAR" if(country_origin==780 & continent=="")
replace continent = "AS" if(country_origin==784 & continent=="")
replace continent = "AF" if(country_origin==788 & continent=="")
replace continent = "EU" if(country_origin==792 & continent=="")
replace continent = "AS" if(country_origin==795 & continent=="")
replace continent = "CAR" if(country_origin==796 & continent=="")
replace continent = "OC" if(country_origin==798 & continent=="")
replace continent = "AF" if(country_origin==800 & continent=="")
replace continent = "EU" if(country_origin==804 & continent=="")
replace continent = "EU" if(country_origin==807 & continent=="")
replace continent = "AF" if(country_origin==818 & continent=="")
replace continent = "EU" if(country_origin==831 & continent=="")
replace continent = "EU" if(country_origin==832 & continent=="")
replace continent = "EU" if(country_origin==833 & continent=="")
replace continent = "AF" if(country_origin==834 & continent=="")
replace continent = "AN" if(country_origin==840 & continent=="")
replace continent = "CAR" if(country_origin==850 & continent=="")
replace continent = "AF" if(country_origin==854 & continent=="")
replace continent = "SA" if(country_origin==858 & continent=="")
replace continent = "AS" if(country_origin==860 & continent=="")
replace continent = "SA" if(country_origin==862 & continent=="")
replace continent = "OC" if(country_origin==876 & continent=="")
replace continent = "OC" if(country_origin==882 & continent=="")
replace continent = "AS" if(country_origin==887 & continent=="")
replace continent = "EU" if(country_origin==891 & continent=="")
replace continent = "AF" if(country_origin==894 & continent=="")
replace continent = "EU" if(country_origin==901 & continent=="")
replace continent = "EU" if(country_origin==902 & continent=="")
replace continent = "EU" if(country_origin==903 & continent=="")
replace continent = "EU" if(country_origin==911 & continent=="")
replace continent = "EU" if(country_origin==912 & continent=="")
replace continent = "EU" if(country_origin==913 & continent=="")
replace continent = "EU" if(country_origin==921 & continent=="")
replace continent = "EU" if(country_origin==922 & continent=="")
replace continent = "EU" if(country_origin==923 & continent=="")
replace continent = "EU" if(country_origin==924 & continent=="")
replace continent = "EU" if(country_origin==925 & continent=="")
replace continent = "EU" if(country_origin==926 & continent=="")
replace continent = "EU" if(country_origin==931 & continent=="")
replace continent = "EU" if(country_origin==951 & continent=="")
replace continent = "EU" if(country_origin==971 & continent=="")
replace continent = "AS" if(country_origin==972 & continent=="")
replace continent = "EU" if(country_origin==973 & continent=="")
replace continent = "EU" if(country_origin==974 & continent=="")
replace continent = "EU" if(country_origin==981 & continent=="")
replace continent = "AF" if(country_origin==982 & continent=="")
replace continent = "AS" if(country_origin==983 & continent=="")
replace continent = "AS" if(country_origin==984 & continent=="")
replace continent = "AN" if(country_origin==985 & continent=="")
replace continent = "MA" if(country_origin==986 & continent=="")
replace continent = "SA" if(country_origin==987 & continent=="")
replace continent = "CAR" if(country_origin==988 & continent=="")
replace continent = "ANT" if(country_origin==989 & continent=="")
replace continent = "Not defined" if(country_origin==990 & continent=="")
replace continent = "Not defined" if(country_origin==991 & continent=="")
replace continent = "Not defined" if(country_origin==992 & continent=="")


*Destringing
local names = "AF ANT AN MA SA AS CAR EU OC"
local i = 0
foreach name of local names {
replace continent = "`i'" if(continent=="`name'")
local ++i
}
replace continent = "9" if(continent=="Not defined")

destring continent, replace

label define continent 0 "Africa" 1 "Antarctica" 2 "North America" 3 "South America" 4 "Middle America" 5 "Asia" 6 "Caribeans" 7 "Europe" 8 "Oceania" 9 "Not defined"
label val continent continent

Advertisements

Estimation sample information from linear regression in R using lm() aka Stata’s: e(sample)

Standard

After running a (linear) regression I regularly want to know the sample size of the sample used in the estimation, the “estimation sample”.

Similarly, I like to be able to identify the observations used in the estimation, e.g. to subset my data. – Stata users know this as the “e(sample)”, which allows one to generate an identifier variable to be used in subsequent operations:

sysuse auto
reg price mpg in 1/70
gen sampleid = e(sample)
label define sampleid 1 "Included in estimation" 0 "Not included in estimation"

R’s lm() function does not produce an object with this information automatically. Instead I have found this useful:

fit <- lm(speed ~ dist, data= cars)
# - N used
esample.n <- nobs(fit)
# - Sample identifier, a set of row names which can be used to subset the corresponding dataframe
esample<-rownames(as.matrix(resid(fit)))
# E.g. subsetting
cars[esample,] #trivial here since all obs are included

 

Stata: How to select a random number from a list.

Standard

I want to randomly select a single value of a variable in Stata in order to e.g. delete it in a simulation/bootstrap.

Stata offers simple ways of creating subsamples from a dataset using:

With replacement use:

bsample

Without replacement:

sample

However, it seems a bit more involved to get a random selection from a list of numbers not in the dataset.

This is what I tried to pick a number from a list of numbers at random – undoubtedly to cumbersome.

1) Create a list of random numbers, which is has as many entries as the list you want to select from

foreach num of local listtoselectfrom { 
 local randomnumber = runiform()
 local rnumbers = "`rnumbers'`randomnumber' "
}

2) Sort the list of random numbers

local selection : list sort renumbers

3) Select the lowest random number

local posofselected = word("`selection'",1)

4) Find the position of this lowest random number in the unsorted list of random numbers

local posinselectionlocal : list posof "`posofselected'" in rnumbers 

5) Use this position to select at random ONE item from the list you want to select from:

local randomitem : word `posinselectionlocal' of `listtoselectfrom'

Example

set seed 7492001 //For replicability.

local listtoselectfrom = " 1 3 4 5 9 10"

foreach num of local listtoselectfrom { 
	local randomnumber = runiform()
	local rnumbers = "`rnumbers'`randomnumber' "
}
local selection : list sort rnumbers
local posofselected = word("`selection'",wordcount("`selection'")) 
local posinselectionlocal : list posof "`posofselected'" in rnumbers 
local randomitem : word `posinselectionlocal' of `listtoselectfrom'
dis "This is the selected number: `randomitem'"

P.S. More than one number
The select more than one number, select more numbers from the random number list in 3). And change loop over their positions using step 4) and 5).

Reference:
The described procedure was inspired by following two posts:
1) http://whatthestats.wordpress.com/2012/08/07/stata-selecting-random-samples/

2) http://blog.stata.com/2012/07/18/using-statas-random-number-generators-part-1/

Merge for Mata – Combining two matrices using a column of id values. (Stata merge for mata)

Standard

 

Occasionally I find myself in the need to combine to matrices of different dimensions from Stata.

(E.g. I estimated svy: proportions over a number of countries for a number of variables and wanted to have one data set containing all results)

In this Stata-list post, the suggestion is to either do this using Stata’s [merge] command or to import the matrix into Mata in loop over all elements.  – It took me quite a while to get this to work, and any feedback would be very welcome.


Example


I want to merge “matm” and “matu” into a matrix “final” using column 1 as id variable.

matm
1 2 +-----------+ 1 | 1 33 | 2 | 2 22 | 3 | 3 11 | +-----------+
matu
     1 2
 +---------+
 1 | 3 2 |
 2 | 1 2 |
 3 | 4 2 |
 4 | 5 2 |
 +---------+
final
     1 2 3
 +----------------+
 1 | 1 33 2 |
 2 | 2 22 . |
 3 | 3 11 2 |
 4 | 4 .  2 |
 5 | 5 .  2 |
 +----------------+

The code below is for a function, which does this using “Mata”.

mata: matamatrixmerge("matm","matu","columwithID1","columwithID2")

This function generates the “final” matrix if it is supplied with:

  • matm: a merging matrix
  • matu: a using matrix
  • columnwidthID1/2: the column number of the column containing the id values for both matm & matu.

State code:


*Define mata function to merge

* – the function expands the two matrices supplied (mergin/using matrix)
* to achieve conformability.
* – The matricies are merge using a single id-variable column.
* [ACHTUNG:] each row needs to be uniquely idenfied.
* [Output:] a merged Stata matrix called “final”.

mata //enter Mata

/*Define mata function and it’s arguments*/

mata drop matamatrixmerge()
function matamatrixmerge(string scalar matm, string scalar matu,string scalar columwithID1, string scalar columwithID2)
{

/*Import Stata matricies into Mata*/

m = st_matrix(matm) //Similar to "merge" there is one merging matrix and
u = st_matrix(matu) // a using matrix - hence matu.
posIDm = st_matrix(columwithID1)
posIDu = st_matrix(columwithID2)

/*Make vector containing all unique values of the ID var*/

idvalues = uniqrows(m[1..rows(m),posIDm]\u[1..rows(u),posIDu])
idvaluesMIN = idvalues[1,1]
idvaluesMAX = idvalues[rows(idvalues),1]

/*Merging:
– The following loop goes over all values of the ID var and
checks these against those supplied in the first matrix.
– If the value does not appear in the supplied matrix,
a row of missing values is added.
– This is done for both matrices supplied.
Result: Two matrices of same dimension which can be row-joined.*/

/*Merge Matrix*/
i=idvaluesMIN
checkedvaluescounter = 0
fm = J(idvaluesMAX,cols(m),.) //matrix collecting expanded mat.
while (i<=idvaluesMAX) {
j = 1
 while (j<=rows(m)) {
 if (idvalues[i,1] == m[j,posIDm]) {
 fm[i,1..cols(m)] = m[j,]
 break
 }
 ++j
 }
++i
}
/*Using Matrix*/
i=idvaluesMIN
checkedvaluescounter = 0
fu = J(idvaluesMAX,cols(u),.) //matrix collecting expanded mat.
while (i<=idvaluesMAX) {
j = 1
 while (j<=rows(u)) {
 if (idvalues[i,1] == u[j,posIDu]) {
 fu[i,1..cols(u)] = u[j,]
 break
 }
 ++j
 }
++i
}

/*Assembles merged matrix*/

f = idvalues,fm,fu //"idvalues column is added" and the now conform matricies joined.

/*Delete colums with duplicated id values */

selectionvectorm = J(1,cols(fm),1)
selectionvectorm[1,posIDm] =.
selectionvectoru = J(1,cols(fu),1)
selectionvectoru[1,posIDu] =.
selectionvector = 1,selectionvectorm, selectionvectoru
f = selectionvector\f
final = select(f, f[1,]:!=.)
final = final[2..rows(final),]

/*Export final matrix from Mata to Stata*/

st_matrix("final",final) //Returns matrix to Stata.
} //end of function definition

/*Save the function in current folder.*/

mata mosave matamatrixmerge(), replace 
end //exit Mata