Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[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 <serjradyakin@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
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>
   svyset[pw=weight]
   <some more Stata stuff>
    svy: mean price // which uses settings remembered in svyset
end

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
user?etc)

Best, Sergiy

On Tue, May 7, 2013 at 1:41 PM, Lacy,Michael <Michael.Lacy@colostate.edu> 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:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index