Statalist The Stata Listserver


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

Re: st: Mata function stata() within program


From   Nick Winter <nw53@cornell.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Mata function stata() within program
Date   Thu, 23 Mar 2006 11:58:07 -0500

This seems right to me. The only thing I'd add is that the plus signs are not "enclosing" the x and y; they are simply the concatenation operator.

In regular Stata, you can do the same thing to put strings together:

. local x = "abc" + "def"

. di "`x'"
abcdef

Or you can concatenate two string variables:

. generate myvar = var1 + var2

and so on.

--Nick Winter


At 11:40 AM 3/23/2006, you wrote:

M Hollis wrote:
> --- "William Gould, Stata" <wgould@stata.com> 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

--
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/
________________________________________________________
Nicholas J. G. Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government nw53@cornell.edu e
Cornell University falcon.arts.cornell.edu/nw53 w
308 White Hall
Ithaca, NY 14853-4601

*
* 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–2020 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index