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

st: RE: Re: statalist-digest V4 #1868

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: statalist-digest V4 #1868
Date   Sun, 30 Jan 2005 17:08:59 -0000

The reason this fails is that 

`x1' `x2' `x3' 

is not a varlist at the time 
you invoke the loop. They are 
just names you have set aside for
temporary variables. In fact, 
you are not even obliged to make 
use of them. 

I would just define each variable 
once round the loop: 

forval i = 1/3 { 
	tempvar x`i' 
	gen `x`i'' = uniform()

[email protected] 

Mike Lacy
> 2) Next, I have occasion to use these tempvars in loops.
> As a simplifed,idealized code fragment to illustrate a confusion
> I have experienced, consider
> tempvar x1 x2 x3
> foreach x of varlist `x1'-`x3' {
>     gen `x' = rand()
> }
> This generates an error message indicating that one of the
> temporary variables is "not found", unless I assign values
> to each of `x1'-`x3' by brute force prior to running the loop 
> and then do a 
> -replace- rather than -gen- in the loop.
> Clearly, I am missing some piece of understanding here as well.
> I guess the question would be:
> How to use a list of temporary variables in a loop if those temporary 
> variable do not yet have assigned values?

*   For searches and help try:

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