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

st: Problem with tempvar __000000 (still confused)


From   "Renzo Comolli" <[email protected]>
To   <[email protected]>
Subject   st: Problem with tempvar __000000 (still confused)
Date   Tue, 22 Jun 2004 11:40:12 -0400

Thanks to both Tom and Nick.

This message shows how to see the error message (part 1) and how this
"feature" can cause -reshape- to fail (part 2)

1. To see the "error" message (which is not a true error message because the
operation is carried on successfully, it is more  a "warning message") do
the following
Step 1: Put the following instructions in a do file
. sysuse auto, clear
. tempvar num_miss
. gen `num_miss'=0
. save debugauto, replace
Step 2: do the do file
Step 3: put the following instructions in the command line
. use debugauto, clear
. twoway (lfit  price  mpg) (scatter  price mpg), by( foreign)

You should now be able to see the "error" message. 
Notice also that the __000000 is there when you -use debugauto- but it has
disappeared after -twoway-. This disappearence is probably intentional and
gives no problem to me, but I thought we need to remark on it "for future
generations".


2. Here is how you can cause reshape to fail
Step 1: Put the following instructions in a do file
use http://www.stata-press.com/data/r8/quad1.dta, clear
replace id=_n // this line is not part of the problem, it is there only to
create a suitable dataset 
tempvar num_miss
gen `num_miss'=0
save debugreshape, replace

Step 2: do the do file
Step 3: put the following instructions in the command line
use debugreshape, clear
reshape long x, i(id) j(number)

I get 
__000000 already defined
variable __000000 not found

Best Regards,
Renzo Comolli


----------------------------------------------------------------------------
----
*From   "Nick Cox" <[email protected]> 
To   <[email protected]> 
Subject   st: RE: Re: Problem with tempvar __000000 
Date   Mon, 21 Jun 2004 09:29:51 +0100 

----------------------------------------------------------------------------
----

I concur with Tom's analysis. Like him, 
I can't reproduce the "already defined" message. 

Note two further details. 

The fact that a temporary variable does not persist 
beyond the end of a do file is explicit within [P] 
macro but may come as a surprise.  

The particular temporary variable name __000000 can only 
I think be reproduced if you have not created 
temporary variables earlier in the same session. More 
generally, Stata avoids names previously 
used in a session even if variables with those names 
no longer exist. Presumably always using a new name 
is the easiest and safest general solution. 

A little experiment you can try is 

forval i = 1/10 { 
	tempvar foo 
	gen `foo' = 1 
	di "`foo'" 
	drop `foo' 
} 

This incidentally illustrates one use of tempvars 
within a main program, to elucidate some programming 
puzzle without writing a named program. 

Nick 
[email protected] 

Tom Steichen
 
> For the situation you describe below, retention of the temp 
> variable in the
> saved dataset is the expected behavior.
> 
> Temp variables are deleted automatically only when their 
> creating program
> (or do file) ends. Thus they remain in existence until dropped or the
> program (or do file) that created them ends.  It follows that 
> if you create
> a temp variable in the "main" program level of Stata, the 
> temp variable
> remains in existence until either dropped or you exit from 
> Stata. If you do
> a -save- without specifying which variables, all variables, 
> including temp
> variables, get saved.
> 
> I tried your example, exactly as you show below but on Stata SE (not
> Intercooled), born the same date you show, and I do not get any error
> message even though __000000 exists.  Thus the entanglement 
> of Graph with
> the temp variable may be an Intercooled problem.

Renzo Comolli
 
> > A variable created by -tempvar- is hanging around inside the saved
> dataset.
> > Its name is __000000
> > Is that normal behavior? I had never noticed this behavior 
> before, but maybe I am just confused.
> >
> > See the (reproducible) code below, taken from my results window.
> > The case presented here is benign, but soemtimes malign cases occur:
> > A. the code gives an error message and still performs the 
> operation, as in
> > the exaple of -twoway- here below
> > B. I believe that at least in one case, it actually broke 
> the code. It was
> > in a -reshape long-. (I can't be sure though, because I was 
> tearing a huge
> > file apart to figure out where the problem was coming from).
> >
> > If, after the code below, you actually issue once again
> > . use debugauto, clear
> > then variable __000000 appears in the variable window.
> >
> > Any clarification? Is it a bug? I am missing something?
> >
> > Best Regards,
> > Renzo Comolli
> >
> >
> >
> > Here is the code to reproduce (most) of the behavior described.
> >
> > . about
> > Intercooled Stata 8.2 for Windows
> > Born 18 May 2004
> >
> > . sysuse auto, clear
> > (1978 Automobile Data)
> >
> > . tempvar num_miss
> >
> > . gen `num_miss'=0
> >
> > . save debugauto, replace
> > file debugauto.dta saved
> >
> > .
> > . use debugauto, clear
> > (1978 Automobile Data)
> >
> > . describe __000000
> >
> >               storage  display     value
> > variable name   type   format      label      variable label
> > 
> --------------------------------------------------------------
> ------------
> --
> > ---
> > __000000        float  %9.0g
> >
> > . tab __000000, mi
> >
> >    __000000 |      Freq.     Percent        Cum.
> > ------------+-----------------------------------
> >           0 |         74      100.00      100.00
> > ------------+-----------------------------------
> >       Total |         74      100.00
> >
> > . twoway (lfit  price  mpg) (scatter  price mpg), by( foreign)
> > __000000 already defined
> >
> > * but the graph still appears correctly


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index