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

RE: st: Re: Programming, rollreg, gaps, and memory


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Re: Programming, rollreg, gaps, and memory
Date   Mon, 18 Apr 2005 22:20:16 +0100

Absolutely. You get what you asked for, 
a new temporary variable. 

The bit that was not obvious to me when 
I learnt this is that 

	tempvar t 

assigns a new name each time. This will make it 
difficult to use any previously assigned
names unless you keep track of them. So 
a common idiom is 

in some loop { 
	tempvar t 
	...
	local tlist "`tlist' `t'" 
} 

That way, each time round the loop, the 
latest name `t' is put in a bag with 
all the previous ones. Thereafter, 
you work with the `tlist'. 

Nick 
[email protected] 

Cameron Hooper
 
> Michael Blasnik wrote:
>  > I think you may need to add:
>  >
>  > drop `res'
>  >
>  > at the end of your loop.
> 
> Hi Michael
> 
> Thanks for your suggestion. I did not realize -tempvar- 
> operated in this 
> fashion. Added the -drop- command fixed that particular 
> problem. (Others 
> have since arisen!) Just to convince myself I wrote the 
> following test 
> program to explore the behaviour of -tempvar-. Other complete newbies 
> might also find it interesting:
> 
> . clear
> . set obs 5
> obs was 0, now 5
> 
> . gen x = _n * uniform()
> 
> . capture drop nv
> . capture program drop tmpv
> 
> . program tmpv
>    1.   qui {
>    2.     gen nv = .
>    3.     forvalues i = 1/5 {
>    4.       tempvar t
>    5.       gen `t' = x[_n]
>    6.       describe
>    7.       replace nv = r(k) in `i'
>    8.     }
>    9.   }
>   10. end
> .
> . tmpv
> . list
> 
>       +---------------+
>       |        x   nv |
>       |---------------|
>    1. | .5415702    3 |
>    2. | .0175157    4 |
>    3. |  .921708    5 |
>    4. | 1.988945    6 |
>    5. | 4.485629    7 |
>       +---------------+
> 
> Yep. Each each iteration of the -forvalues- loop generates a 
> new variable.

*
*   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