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

RE: st: RE: Tempname and Tempvar - help desperately needed


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: Tempname and Tempvar - help desperately needed
Date   Wed, 19 Jun 2002 17:25:25 +0100

Maureen Paul
> 
> Thanks very much for your help. However, permit me to restate my 
> problem. It is like this: If I do not specify "tempvar y`x'iqq 
> y`xless1'iqq;" within the loop as below, I get the error message that 
> "too few variables specified" which after using 'set trace on' and 
> 'mat li' translates into "global macro $ge is not recognised" or 
> something like that. On the other hand, including it solves that 
> problem but introduces another - looping through encounters tempvar 
> again and yields the error message that the variable has already been 
> created. 
> 
> I suppose, what I would like to know is whether tempvar and tempname 
> cannot be used in the way that I am trying to and if so, is there any 
> other way I can get the programme to automatically drop variables 
> without me having to explicitly tell it to do so? As you can see 
> below, the names y`x'iqq y`xless1'iqq are set to change as x and 
> xless1 changes.
> 
> 
>  if `j'~=`depmax' { ;
> 		tempvar y`x'iqq y`xless1'iqq; 
> 		capture drop `y`x'iqq' ;       
> 		if _rc == 0 { local dropvar="`dropvar' `y`x'iqq`" } ;
> 		ge `y`x'iqq' = 0 `if' ;
> 		replace `y`x'iqq'=( `phiwj' ) / ( `PHIwj' - 
> `PHIwj_1' ) if `1'==`j' 
> & `ifexp' ;
> 	} ;

Sorry, I can't follow most of this, but in your code
fragment you are trying to drop a temporary variable before it 
has been created. I think Stata can only object. 

The heart of the matter can be shown by trying to run this: 

tempvar foo 
drop `foo' 
gen `foo' = 0

and you can do this interactively. 

What -tempvar- does is basically set up a name for you to refer to when 
you want to -generate- or (later) use a temporary variable. 
It does not create that variable. Thus you won't get past the -drop- 
command: there is no variable `foo' to -drop- at that point. 

In fact, you could also use -tempname- for this, which makes the 
logic of what you are doing a bit clearer: 

tempname foo
drop `foo' 
gen `foo' = 0 

Nick 
[email protected] 
*
*   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