Statalist


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

Re: st: RE: Help with loops


From   "Rijo John" <rmjohn@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Help with loops
Date   Thu, 15 May 2008 16:33:13 -0500

Thanks Nick. It worked perfect.

On Thu, May 15, 2008 at 4:03 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> Dots are not allowed in variable names. Period.
>
> So your code could read something like this:
>
> local varlist1 "var1 var2 var3 var4"
> forval i = 1/3 {
>        foreach X of local varlist1 {
>        gen `X'_y`i' = `X'_y
>                replace `X'_y`i' = `X'_y + (`X'_y * `i'/100)
>        }
> }
>
> It's not a good idea to loop over lists like 0.01 0.02 0.03, as sooner
> or later precision problems will bite. I always loop over integers, and
> do any arithmetic yielding fractions _inside_ the loop.
>
> Notice that I changed your outer loop to -forval-. That's a matter of
> style, not syntax.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Rijo John
>
> I have the following that gives results the way I wanted.
>
> local varlist1 "var1 var2 var3 var4 "
> foreach i in 1 2  3{
>        foreach X of local varlist1{
>            gen `X'_y`i'=`X'_y
>            replace `X'_y`i' = `X'_y+(`X'_y*`i')
>        }
> }
>
> However, if I change values 1, 2, 3 to 0.01, 0.02 and 0.03 it gives me
> the error saying "var1_y0.01 invalid name". As I understand variables
> can not be named with dots in it? if so how do I get around? how can I
> modify it to generate variabes such as var1_y1=var1-(var1_y*0.01) and
> so on.
>
> *
> *   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/
>
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index