Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: RE: loops


From   Hawal Shamon <hawalshamon@hotmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: loops
Date   Wed, 21 Dec 2011 12:37:29 +0100

Thanks Jesper,

following worked!! 


local i 1
while `i' <=16 {
foreach var of varlist AB01_01-AB01_16 {
encode `var', gen(AB`i')
local i=`i'+1
}
}


Cheers,
Hawal

----------------------------------------
> Subject: st: RE: loops
> Date: Wed, 21 Dec 2011 12:22:28 +0100
> From: JESLIN01@geh.regionh.dk
> To: statalist@hsphsun2.harvard.edu
>
> Hi there
>
> You seem to miss to increment local I in your loop, eg, add
>
> local i=i+1
>
> before exiting the loop.
>
> Hth,
>
> Jesper
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Hawal Shamon
> Sent: 21 December 2011 12:10
> To: statalist@hsphsun2.harvard.edu
> Subject: st: loops
>
>
>
> Dear all!
>
> 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.
>
> Can anyone help?
>
> Thanks in advance!
>
> Best,
>
> Hawal
> *
> * 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/
 		 	   		  
*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index