Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: replacing task in a loop

 From Daniel Marcelino To statalist@hsphsun2.harvard.edu Subject Re: st: replacing task in a loop Date Fri, 29 Jul 2011 09:10:36 -0400

```Thanks Phil,

Daniel

On Fri, Jul 29, 2011 at 1:58 AM, Phil Clayton
<philclayton@internode.on.net> wrote:
> Why not just use -recode-?
>
> eg:
> recode `v' (1=0) (2=0.25) (9=0.5) (3=0.75) (4=1)
>
> From the version 11.2 manual entry for -recode-:
> recode evaluates the rules from left to right. Once a value has been transformed, it will not be
> transformed again. Thus if rules “overlap”, the first matching rule is applied, and further matches are
> ignored.
>
> Phil
>
> On 29/07/2011, at 11:42 AM, Daniel Marcelino wrote:
>
>> Dear all,
>> I have a doubt on the looping process that might be simple to solve. I
>> have a sort of variables with the same scales 1 to 9 that I want to
>> recode their values to a different scale. However, when I do a loop
>> for replace these values I got some confusing values in particular for
>> the value  1. To deal with this problem I using a not pretty
>> solution: I'm running  2 loops over the variables as follow.
>>
>> foreach v of varlist q* pq* {
>> replace `v' = 0 if `v' ==1
>> replace `v' = 0.25 if `v' ==2
>> replace `v' = 0.50 if `v' ==9
>> replace `v' = 0.75 if `v' ==3
>> replace `v' = 0.9 if `v' == 4
>>  }
>>
>> /* I'm loop again because I cannot transform 4==1 in the same previous
>> loop. Therefore, I replaced 4==0.9 and now 0.9 == 1
>>
>>
>> foreach v of varlist q35a* pq26a* {
>> replace `v' = 1 if `v' ==0.9
>>   }
>>
>> My doubt is: there is a way to replace all values without the risk of
>> the new value be considered again in the loop?
>>
>>
>> Daniel
>>
>> *
>> *   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/
>

--