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]

From |
tashi lama <ltashi32@hotmail.com> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: beginer's mata function in dofile |

Date |
Tue, 7 Aug 2012 15:23:16 +0000 |

thanx a lot. This will definitely upp me to try using more mata in my program. The only thing I didn't follow is where you say... it is not necessary to define a program that calls mata functions. Could you elaborate on that? Thanx ---------------------------------------- > Date: Sat, 4 Aug 2012 14:28:20 +0200 > Subject: Re: st: beginer's mata function in dofile > From: klein.daniel.81@googlemail.com > To: statalist@hsphsun2.harvard.edu > > 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/ * * 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/

**References**:**Re: st: beginer's mata function in dofile***From:*daniel klein <klein.daniel.81@googlemail.com>

- Prev by Date:
**Re: st: Loop-within-loop error while looping over observations for a microsimulation** - Next by Date:
**RE: st: RE: IV with many fixed effects** - Previous by thread:
**Re: st: beginer's mata function in dofile** - Next by thread:
**Re: st: beginer's mata function in dofile** - Index(es):