Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: beginer's mata function in dofile


From   daniel klein <[email protected]>
To   [email protected]
Subject   Re: st: beginer's mata function in dofile
Date   Sat, 4 Aug 2012 14:28:20 +0200

Tashi,

your code is kind of confusing. It seems you should have a look into
Stata ado-programming, before starting with Mata. Your problem is not
a Mata specific one, but  you need to understand the basic principles
of Stata programming. I recommend reading Baum (2009) chapters 12 and
13 for an excellent introduction.

Even though it is not necessary to define a program that calls Mata
functions, it seems this is what you are trying to do here. Before
giving a short outline on how to do that, I would like to quote from
Gould (2006, 114)

"Given trends(), one solution to our problem would be

mata: do_trend_var("b", "bp1 bp2 bp3 bp4 bp5")

_after_ defining

function do_trend_var(string scalar newvarname, string scalar varnames)
[...]
(emphasis mine)

So the basic idea should be obvious. You need to define the function
<do_trend> in Mata, _before_ it can be called from within Stata. This
is exactly what Stata is telling you (admitted that error messages in
Mata could be a little more specific from time to time).

Let's get back to your example. First you -capture drop- the program
<nn>. However this program is never defined, nor is any other program
(hence my advice to read Baum (2009)).  In your code the Mata function
is defined after the first call to it (ignoring the fact, that your
code will not define the function, because your -end- does not end
anything and you do not invoke Mata, definig the function). The
outline for a program calling Mata functions looks like this

[cap pr drop foo]
[mata : mata clear]

pr foo
    version 12.1
    my_function()
    [...]
end

version 12.1
mata :
my_function()
{
[...]
}
end

You will have to save this file as foo.ado, where Stata can find it.
When you then first call -foo- from within Stata, Stata loads foo.ado
into memory, meaning that it defines Stata program foo and Mata
function my_function(). If you are not willing to save the file, you
can -do- (or -run-) the complete code once. This will also define the
programs and Mata functions (actually it compiles the Matat function,
if my understanding of Mata is correct). There are other ways to go,
but you might want to do some reading on this yourself, maybe starting
with -help m1_ado-. Either way, the Mata function must be defined
(compiled), before it is called.

I could go on and wirte more, but I feel this is merely repeating what
is already documented elsewhere (e.g. type -h mata- and do some hours
of reading, following the examples given).

Best
Daniel


Baum, C., F.. (2009). An Introduction to Stata Programming. College
Station, Texas: Stata Press.
Gould, W. (2006). Mata Matters: Creating new variables - sounds
boring, isn't. The Stata Journal, 6(1), pp. 112-123.

-- 
Hello all,
    I am trying to learn Mata and this is my first do file with mata
function. I mimicked the code originally written by  William Gould of
StataCorp titled "Mata Matters: Creating new variables-sounds boring,
isn't".

[...]
begin do-file mata1.do
capture program drop nn
mata: do_trend_var("b", "bp1 bp2 bp3 bp4 bp5")
list
end
function do_trend_var(string scalar newvarname, string scalar varnames)
{
real matrix Y
real colvector b
real scalar idx
st_view(Y, ., varnames)
b =1\2\3\4\5
idx = st_addvar("float", newvarname)
st_store(., idx, b)
}
exit
nn

when I run, stata isn't happy and throws back
 <istmt>:  3499  do_trend_var() not found
r(3499);

Any idea?
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index