Bookmark and Share

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

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

Re: st: Avoiding use of Mata external variables in large programs

From   Sergiy Radyakin <>
To   "" <>
Subject   Re: st: Avoiding use of Mata external variables in large programs
Date   Tue, 7 May 2013 18:57:52 -0400

Dear Mike,
I see similarity to the way svy commands are implemented in Stata:

prog MyStataProg
    sysuse auto
   <some Stata commands>
   <some more Stata stuff>
    svy: mean price // which uses settings remembered in svyset

SVY stores the settings among the dataset characteristics. Type -char
list- before and after svyset to see what it adds. These are the
_dta[_svy_*] settings. Note that the manufacturer (StataCorp) doesn't
give you any warning when there is a collision at svy. Apparently the
user should not use "_"-prefixed chars, the use of which is reserved
for programmers. This is a reasonable assumption and is safe "almost
always" :)

If this is not suitable, there are other alternatives, but then I need
more info, how much of those settings you need to save? how much
performance matters? how much security matters? (are you protecting
data from accidental collisions? protecting something from the

Best, Sergiy

On Tue, May 7, 2013 at 1:41 PM, Lacy,Michael <> wrote:
> I'm seeking advice about avoiding the use of Mata external variables in
> Mata/Stata adofile programs.  This use arises in passing intermediate
> results among different Mata functions, as I illustrate below, but there
> are occasions where this might arise. My concerns have to do with
> keeping Mata variables and their names as limited in scope as possible,
> to avoid me or someone else accidentally clobbering them.
> My general approach in using Mata is what I presume as typical, i.e. to  use a
> Stata program as the main program, and call Mata functions within it.  I find
> it convenient to use Mata external variables to retain Mata results until
> the next Mata function needs them.  Schematically, we might have something like:
> prog MyStataProg
>    <some Stata commands>
>   mata: E1 = MyMata1(...)
>    <some more Stata stuff>
>    mata: MyMata2(...)  // which uses E1 and get access to it by declaring E1 as external
> end
> I think of having E1 visible at the "main" level Mata as undesirable.  Further, My E1 could
> step on a variable name used in Mata by me or someone else.
> My thought for avoiding this are:
> 1) Use Stata's -tempname- to declare E1
> tempname E1
> mata: `E1' = ...
> 2) Make a Mata function as the main program, so that E1 is within it:
> mata:
> MyMainMataProgram() {
>   stata(cmd1)
>   E1 = MyMata1()
>  stata(cmd2)
>   MyMata2(E1, ...)
> }
> =========================
> Both options 1) and 2)  seem unattractive.  Can others comment on these options,
> including using "unhidden" externals as I suggested in my schematic, and/or suggest
> something else?  I understand that I could retain some intermediate results like E1
> as Stata scalars or matrices, but that is often clumsy or impractical.
> Regards,
> Mike Lacy
> Dept. of Sociology
> Colorado State University
> Fort Collins CO 80523-1784
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

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