### Wednesday, March 22, 2006

## Re: st: Mata function stata() within program

Many thanks for this. I will carefully look at it. It seems that this will make the program much faster, which would be _very_ important. The snippet will often run more than a million times ...

William Gould, Stata wrote: > Ulrich Kohler <kohler@wz-berlin.de> wrote, > > > I have a Mata function which looks as follows: > > > > -------------------------------------lsq.mata-- > > (...) > > // Mata Function to extract the substitution costs from subcost-matrix > > void showhash(real rowvector R) > > { > > string scalar key1 > > st_local("key1",key1) > > key1 = strofreal(R[1,2]) > > st_local("key1",key1) > > stata("local hash1 = mod(`key1',197)") > > } > > > > (...) > >------------------------------------------------ > > and Uli notes that when he runs it, he gets an error, > > > : R = 2,3,5,4 > > : showhash(R) > > > > invalid syntax > > stata(): 3598 Stata returned error > > showhash(): - function returned error > > <istmt>: - function returned error > > r(3598); > > Alan Riley <ariley@stata.com> has already given a solution, and suggested > the line > > stata("local hash1 = mod(`key1',197)") > > be changed to read > > stata("local hash1 = mod(\`key1',197)") > > Alan's right, but his solution is too tricky for me. Moreover, his > solution shows he is still thinking an ado mode rather than Mata mode. > > My suggested solution is > > stata("local hash1 = mod(" + key1 + ", 197)") > > and, with my solution, Uli's code can be simplified to read, > > > void showhash(real rowvector R) > { > string scalar key1 > > key1 = strofreal(R[1,2]) > stata("local hash1 = mod(" + key1 + ", 197)") > } > > or even > > void showhash(real rowvector R) > { > stata("local hash1 = mod(" + strofreal(R[1,2]) + ", > 197)") } > > Let me expound on the ado versus the Mata way of thinking. > > Uli wanted to run the Stata command > > local hash1 = mod(_______, 197) > > where he substituted the value from Mata matrix R[1,2] for ______. > Why Uli wanted to do this, we don't know, nor care. > > The ado way of thinking says we substitute a macro for _____, and arrange > for the macro to contain R[1,2], so when the macro is substituted by > Stata, we obtrain the desired result. Good way of thinking, when you > are writing an ado-file. > > The Mata way of thinking is more direct: we need to construct a string > > "local hash1 = mod(_______, 197)" > > where where R[1,2] is substitued for _____, and we can just use the > standard operators to do that, > > "local hash1 = mod(" + strofreal(R[1,2]) + ", 197)" > > Here's a good rule: It's perfectly okay to obtain input from macros, or > post output to macros. That is one way Mata can communicate with > ado-files. If you have to use macros to obtain your result, however, you > are thinking ado, not Mata. There's a simpler, more direct way. > > -- Bill > wgould@stata.com > * > * For searches and help try: > * http://www.stata.com/support/faqs/res/findit.html > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/

-- kohler@wz-berlin.de +49 (030) 25491-361 * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

Tag: statalist