Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: Errors "out of range" for a simple Mata function


From   Ly Tran <lyhuyen@brandeis.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Errors "out of range" for a simple Mata function
Date   Mon, 19 Nov 2012 12:05:53 -0500

Hi there,
I am trying to write a simple Mata function, mostly to learn about the
functions themselves, but run into a problem that I fail to solve.
The function creates a new variable that is the row sum of several
existing variables.
Here it is:
capture program drop testsum
program testsum, rclass
syntax varlist(numeric) [if] [in], GENerate(string) [FLOAT]
qui generate `float' `generate' = .
mata: tsum( "`varlist'", "`newvar'")
end

mata
void function tsum (string scalar varlist, string scalar newvar)
{
real matrix X, Z
st_view(X = .,., tokens(varlist))
st_view(Z =., ., newvar)
Z = rowsum(X)
}
end
testsum AUD JPY, gen(test)
The error is tsum(): 3301 subscript invalid
<istmt>: - function returned error

I figured that for some reason, after the line st_view (Z = .,.,
newvar), somehow the cols(Z) returns 0, and I think that's where the
error comes in.
When I avoid using mata function but embed this piece of mata code in
the program (minus the line that declares matrices), it runs just fine
and returns what it is supposed to.
Can anybody please help me with this? Any idea how this can be fixed
would be much appreciated.

--
Ly Tran
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index