Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Mata function stata() within program


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: Mata function stata() within program
Date   Thu, 23 Mar 2006 17:40:13 +0100

M Hollis wrote:
> --- "William Gould, Stata" <[email protected]> wrote:
> > My suggested solution is
> >
> > stata("local hash1 = mod("  +  key1  +  ", 197)")
>
> Thanks for your detailed discussion about mata vs. ado
> files.  Very helpful.  I was wondering, though, if you
> could provide a manual reference or more of a
> description of the syntax in your suggested solution
> above.  In particular, I'm wondering what the " + key1
> + " is doing exactly.  I haven't run across that
> option in the manual, and it's hard to search the help
> for something that's just quotes and plus signs.  I
> feel like it could be a very useful function if I
> could figure out how it works.

Not that I am competent, but I think I can answer your question partly. Bill 
might step in to correct me if I were wrong. 

In general, you have to put a string within the parentheses of stata(), hence 
you have to state:

: stata("local hash1 = mod(1,197)")

Now, consider the situation that you want to change the code to something 
saying

: stata("local hash1 = mod(x,y)")

where x and y  are Mata variables. As it stands x and y not get "expanded", 
because they are enclosed in quotes. Hence Stata would see 
-local hash1 = mod(x,y)- which will give an error. Bills trickery now is to
close the quotation before x, than plug in x as a Mata Function, and finally 
put the rest of the command in quotes again. Hence is code should read as

: stata(
        "local has1 = mod"   // <- within qoutes
           x                 // <- without qoutes
         ", "                // <- within qoutes 
           y                 // <- without qoutes
         ")"                 // <- within quotes
        )

But that is only the first part of the idea. The second part is, that x and y 
has to be "enclosed" between + signs.

: stata(
        "local has1 = mod"   
         + x +               // <- within + 
         ", "                
         + y +               // <- within + 
         ")"                  
        )

Don't ask me why, but it works ;-). Probably that way Mata knows that it has 
to calculate something. Maybe someone else can take over for the explanation.

Uli

-- 
[email protected]
+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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index