Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: How to reference tempvars across ML programs?

From   "Nick Cox" <>
To   <>
Subject   st: RE: How to reference tempvars across ML programs?
Date   Thu, 10 Oct 2002 18:02:17 +0100

Stephen P. Jenkins

> ML programs written in ado file form comprise a main program and
> another program (or programs) called each iteration to evaluate the
> likelihood. Advice please about how to reference in the likelihood
> evaluation program(s), temporary variables that are created
> in the main
> program.
> More details:
> In myp1.ado I currently have code like
> 	generate z`i'`d' = uniform()
> generated in a loop, where `i' = 1,...,$M, and `d' =
> 1,...,$D (and both
> $M, $D are user-defined).  The -ml- command in this file calls
> myp1_ll.ado in which I do calculations referring to each variable
> z`i'`d', again within a loop over `i' and `d'.
> I can make the program work in this way, but note that the
> "z" prefix
> means that I am generating permanent variables with a specific name
> (a Bad Thing as far as programming is concerned).


> How can I make these indexed variables temporary but still refer to
> them correctly?
> I've played around with using -tempvar- commands within
> myp1.ado, but I
> don't seem able to refer to these (suitably indexed) in
> myp1_ll.ado.
> (Note too Manual [P] macro entry, p. 133, that warns that
> one cannot
> suffix temporary variables with additional characters and use them.)

I have some thoughts on this point, although they are
perhaps orthogonal to Stephen's question.

In fact, you _can_ do this in Stata 7, and I have often
done it, as it lets me do things which I find difficult
otherwise. I'm aware that this is a bug, or perhaps
a loophole, and that some future version of Stata
may close the loophole and break the programs in

What has happened is although permitted variable
names can be much longer in 7 (32 chars), temporary
names are all still 8 characters long. So you can attach
characters to tempnames, and, more important, Stata acts as if
variables created with such names are just like regular
temporary variables.

Here is a simple example:

tempname foo
gen `foo'123 = 123

This works. Also, the variable in question
is dropped when the program finishes.

Why do I want to do this?

Say I want to -separate- something within a program.
The resulting variables are just needed temporarily.
-separate- takes a stub for the names of
variables to be created. In practice,
a tempname does work, and I haven't
thought of an easier solution.

If this is taken away with one hand by Stata Corp,
then I hope they give back another way of doing this
with the other. I have mentioned this to Stata
Corp personnel.

> Another idea was to use global macros to hold the variable names
> created, but again I couldn't get this to work.

In principle, this should work. Any details?


*   For searches and help try:

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