Statalist The Stata Listserver


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

Re: st: Saving to locals in Mata


From   Alan Riley <ariley@stata.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Saving to locals in Mata
Date   Wed, 25 Apr 2007 12:59:00 -0500

Julian Reif (julian.reif@bateswhite.com) asked about saving locals
from Mata to Stata:
> I am having trouble saving to locals using Mata. The following ADO code demonstrates my problem:
> 
> ***example.ado code***
> 
> 	program example
> 		version 9.2
> 
> 		mata: example()
> 	end
> 
> 	mata:
> 		void example()
> 		{
> 			t = "string"
> 			st_local("my_local",t)
> 			stata(`" di "my_local is `my_local'""')
> 		}	
> 	end
> 
> ***end example.ado code***
> 
> my_local is displayed as blank instead of "string". However, if I run the three lines of mata code inside of example() interactively, my_local has the contents "string". Why does this work interactively but not in my program? How should this have been coded so that it works?
> 
> The same problem happens if I use globals instead of locals.

Kit Baum and Maarten Buis suggested looking at the value of the
local after the Mata function example() was through.

The local actually exists in Stata immediately after Mata created
it with

   ...
   t = "string"
   st_local("my_local",t)
   ...

Julian could prove this by adding the line

   stata("macro list")

immediately after the call to st_local().  So, if my_local has been
created when the Mata function example() runs, why doesn't the
call to -display- from the stata() function work?

The reason Julian is not able to 'see' it in Stata with this line

   stata(`" di "my_local is `my_local'""')

is because what gets passed to Stata to be executed is not what
he thinks.

Remember that Mata is a compiled language, and for it to be
compiled, it must be first read by Stata.  When Stata reads a line
of Mata code, Stata AT READ TIME applies its normal macro substitution
rules.  When the line

   stata(`" di "my_local is `my_local'""')

was initially read by Stata (while it was acquiring lines of the
example() function to be compiled), my_local did not exist.  Thus,
what was actually seen to be compiled was

   stata(`" di "my_local is ""')

When that line is later executed, of course all that is output by Mata
is

   my_local is 

Julian needs to make sure that the literal string

   di "my_local is `my_local'"

is seen by Stata when it is reading and compiling the Mata code.  He
can do this by protecting `my_local' from macro expansion with a
backslash:

   stata(`" di "my_local is \`my_local'""')


Then, at read/compile time, Stata won't try to expand the non-existent
macro my_local and the literal string

   di "my_local is `my_local'"

will be stored in the compiled Mata function example() to be passed
to the stata() function for execution later.


Alan
(ariley@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/



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