Hello -
I'm working in Stata version 10 on a Mac.
I've just written my first function in mata and I need some help. My function takes a column vector of length r, and creates a r x r matrix with elements that equal 1 if two elements of the column vector are the same, and zero otherwise. The function works well, except for the fact that it returns the r x r matrix to the screen. Since the vectors that I need to input are 5000-6000 rows long, this is a problem. I've tried typing "quietly" before I execute the function in mata, and that returns the error:
invalid expression
r(3000);
I've tried adding the word "quietly" before the return line in my function, i.e.:
quietly return(WDY0)
but then I got this error message:
'return' found where almost anything else expected
I've looked through the "programing" and "mata" documentation, along with Stata help and the statalist archives (and Google) and can't figure out a solution to this problem. If anyone has an answer, it would be much appreciated. I've included the text of my program below.
Thank you for your consideration,
Kramer
*this is a do-file to create the function "district"
* "district" takes the district number observation from each school and
* creates a matrix whose elements are one if school i != school j AND if
* school i and school j are in the same district
*INPUTS: T = column vector of district numbers from a given year
* OUTPUT: WDY0 = matrix whose elements are one if school i != school j AND if
* school i and school j are in the same district
mata:
real matrix district(real colvector T)
{
real scalar r
real matrix A
real matrix B
real scalar i
real matrix WDY0
r = rows(T)
A = J(r,r,0)
for(i=1; i<=r; i++){
A[i,.]=T'
}
B = J(r,r,0)
for(i=1; i<=r; i++){
B[.,i]=T
}
WDY0 = A - B
for(i=1; i<=r; i++){
for(j=1; j<=r; j++){
if((WDY0[i,j]==0) & (i!=j))
WDY0[i,j] = 1
else WDY0[i,j]=0
}
}
return(WDY0)
}
mata mosave district(), dir(/Users/Kramer/Desktop/Thesis/) replace
end
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/