Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: st: loops
From
Hawal Shamon <[email protected]>
To
<[email protected]>
Subject
RE: st: loops
Date
Wed, 21 Dec 2011 12:42:53 +0100
Thanks a lot Nick!
Due to your explanation (1.) I got the idea why an incremental is necessary. I also like your suggested alternative (2.) and your hint (3.).
Cheers,
Hawal
----------------------------------------
> Date: Wed, 21 Dec 2011 11:31:57 +0000
> Subject: Re: st: loops
> From: [email protected]
> To: [email protected]
>
> You have several small misunderstandings at once.
>
> 1. Using -while- does not imply by itself that its argument is
> automatically incremented within the loop. Thus second time around the
> loop the local -i- is still 1 and the name -ABD011- is already taken,
> hence the error.
>
> 2. There is in essence one loop here, a loop over 30 variables. This
> should do it
>
> foreach var of varlist AB01_01-AB01_30 {
> local newvar : subinstr local var "_" "", all
> encode `var', gen(`newvar')
> }
>
> Nothing stops you stepping through two loops simultaneously, as you
> were trying to do, but it is not necessary.
>
> 3. A more subtle potential problem is that you were going to -encode-
> your variables separately. This does not guarantee consistent
> encodings unless all the distinct values in all the variables occur in
> each distinct variable. -multencode- (SSC) is a solution to this
> problem. Defining value labels ahead of the -encode- is another.
>
> 4. There is some implied confusion between -encode- and -rename-.
> -encode- creates new variables and does nothing to the originals. As
> new variables they need new names; this is nothing to do with
> renaming.
>
> Nick
>
> On Wed, Dec 21, 2011 at 11:10 AM, Hawal Shamon wrote:
>
> > I have problems in finding the correct syntax for a nested loop.
> >
> > I
> > have the string variables AB01_01, AB01_02, ..., AB01_30 and like to
> > encode and rename them at the same time. Renaming is necessary to get
> > rid of the "_" in the variables name. Do the result is supposed to be
> > AB0101, AB0102, ..., AB0130.
> >
> > I tried it with:
> >
> > local i 1
> > while `i' <=30 {
> > foreach var of varlist AB01_01-AB01_30 {
> > encode `var', gen(AB01`i')
> > }
> > }
> >
> > and received the following error message:
> > .AB011 already defined
> >
> > I understand the problem. However, I have no clue how to avoid this problem.
> >
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/