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

st: RE: Re: nested foreach to rename variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: nested foreach to rename variables
Date   Wed, 16 Oct 2002 09:46:48 +0100

Buzz Burhans

> > I would appreciate any pointers on how to get this loop
> to work. After
> > using insheet I have variables named var3, var4,
> var5...var14 which
> > correspond to measurements made on day  18, 15, 12, 9, 7,
> 6, 5, 4, 3, 2,
> 1,
> > 0 before an event.  I have used the following to rename
> the variables,
> > unsuccessfully.  It renames v3 to preday18, but names v4
> to preday0 and
> > then fails on v5 with the error message "preday0 already
> defined.  it
> seems
> > I'm losing the value of `d2' generated in the nested
> "foreach num" loop
> > when it returns to the first "foreach var" loop. perhaps
> it's just late,
> > but i can't seem to come up with a variation that works.
> >
> > My construct is:
> >
> > local d = 18
> > local d2 string(`d')
> > foreach var of varlist v3-v14 {
> > renamev `var' "preday"+ `d2'
> > foreach num of numlist  15 12 9 7 6 5 4 3 2 1 0 {
> > local d = `num'
> > local d2  string(`d')
> > }
> > }

Scott Merryman gave a good solution. In fact,
this is one kind of problem in which -for-, despite
its many disadvantages, remains competitive with
-foreach-:

> How about simpler construction:
>
> for var v3-v14  \ num 18 15 12 9 7 6 5 4 3 2 1 0: rename X predayY

Another solution is just to use -renvars- from STB-60:

renvars v3-v14 \ preday18 preday15 preday12 preday9 preday7 <and so
on>

There are other -renvars- solutions, apart from just spelling
out all the names, but by the time you have worked out
a cleverer syntax you might just as well as typed out
all the names.

Buzz's code poses various problems.

local d = 18
local d2 string(`d')
foreach var of varlist v3-v14 {
	renamev `var' "preday"+ `d2'
	foreach num of numlist  15 12 9 7 6 5 4 3 2 1 0 {
		local d = `num'
		local d2  string(`d')
	}
}

* d, d2 and num are all the same thing, and you
only need one. (If you use -string()-, you need
an = sign to its left.)

* -renamev- is presumably a typo for -rename-,
which is not going to like the syntax you give
it.

* I can see no reason to treat 18 differently.

* The nested structure is wrong for this
problem. Nested structures looping over
I cases (outer loop) and J cases (inner loop)
cycle through I * J cases, not what you
want here. You need a parallel structure,
a single loop.

You could use -foreach- for this problem,
but I can't immediately see a way to
do it that would be simpler than Scott's
-for- solution. Your names are a little too
irregular to allow a neat solution.

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