The problem is that you are trying to rename a variable with parentheses.
-set trace on- shows that the problem is the local newname contains
"substr(x1, 6, .)" :
. clear
. input str20 x1 str20 x2
x1 x2
1. "abcdefghij" "dsfskfslkdfsdf"
2. "abcdefghijklmno" "gjjgdfdfefdf"
3. end
. set trace on
. foreach var of varlist x1- x2 {
2. local newname substr(`var', 6, .)
3. rename `var' `newname'
4. }
- foreach var of varlist x1- x2 {
- local newname substr(`var', 6, .)
= local newname substr(x1, 6, .)
- rename `var' `newname'
= rename x1 substr(x1, 6, .)
( not allowed
Change the line so that uses the inline macro expansion:
local newname `=substr(`var', 6, .)'
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Raphael Fraser
> Sent: Monday, October 30, 2006 3:49 PM
> To: [email protected]
> Subject: st: Foreach Problem
>
> Can some one explain why the following code does not work?
>
> . foreach var of varlist haem_id - haem_nbc {
> 2. local newname substr(`var', 6, .)
> 3. rename `var' `newname'
> 4. }
> ( not allowed
> r(101);
>
*
* 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/