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

st: nested foreach to rename variables


From   Buzz Burhans <wsb2@cornell.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: nested foreach to rename variables
Date   Tue, 15 Oct 2002 22:31:08 -0400

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')

}


}


Thanks

Buzz Burhans
Dept. of Animal Science
Cornell University
*
* 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index