Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Foreach Problem


From   "Raphael Fraser" <[email protected]>
To   [email protected]
Subject   Re: st: RE: Foreach Problem
Date   Mon, 30 Oct 2006 17:53:45 -0500

Didn't work. From the trace as you said -substr(`var', 6, .)- is not
being evaluated as I expected. Typing the following in the command
line works:

. local newname substr("haem_id", 6, .)
. di `newname'
id

However, typing the foll. does not work:

rename haem_id `newname'
( not allowed

trace on
foreach var of varlist haem_id - haem_nbc {
- local newname `=substr(`var', 6, .)'
type mismatch
= local newname
- rename `var' `newname'
= rename haem_id
varname required
 }
r(100);


On 10/30/06, Scott Merryman <[email protected]> wrote:
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/


--
=============================
Raphael A. Fraser, MSc
Assistant Lecturer in Biostatistics
Sickle Cell Unit
Tropical Medicine Research Institute
Faculty of Medical Sciences
University of the West Indies
Kingston
Jamaica

Tel:  (876) 927-2471; 410-4699
Fax: (876) 927-2984
e-mail: [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