Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Using stata local macro in mata


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Using stata local macro in mata
Date   Mon, 9 Sep 2013 19:46:00 +0100

A short answer here is that you are better off, long run, forgetting
all about Stata local and global macros when you are using Mata. The
existence of functions to pass back and forth is is more likely to be
a distraction than a help. This especially applies to code that will
be compiled.

In this case, I guess this is your problem. You have a do-file called
within a loop. But once the do-file is being run, only its locals are
visible. The locals in the calling program or do-file are invisible.
That's what "local" means: local to a program, do-file, do-file editor
contents or interactive sessions (which I call "locales", but that's
not  official terminology).

So, what you can do is pass the value of k to your do-file.

do rou.do `k'

and within your do-file, the first line can be

local k `1'

Conversely, you can see why your interactive example works. There is
no immediary do-file.

It would be better style, however, to write your Mata code as a
function and pass the value you want to that function as an argument.

Nick
[email protected]


On 9 September 2013 19:20, Benjamin Villena <[email protected]> wrote:
>
> Dear Stalist members,
> My problem is as follows. I try to run a do file (let's call it rou.do) that contains
> a mata routine using a parameter value kappa.
> I would like to run rou.do several time using different values for kappa so
> I built a forv loop such as
>
> forv k=1/10{
>      do rou.do
> }
>
> In rou.do, the first lines read like this
>
> mata
> kappa = strtoreal(st_local("k"))
>
> However, I cannot make mata to read the local k correctly. The mata code
> crashes because of that. While debugging in mata, the value of kappa is
> just missing (a dot)
>
> If I do this directly in stata's command line by typing
>
> local k=1
> mata
> kappa = strtoreal(st_local("k")),
>
> kappa's value is 1.
>
>
> What's wrong with my approach? How could I "import" a local macro in mata
> correctly?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index