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

From |
"n.j.cox@durham.ac.uk" <n.j.cox@durham.ac.uk> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: Help with loops |

Date |
Fri, 16 May 2008 07:40:02 -0700 |

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/ _____________________________________________________________________ Mensaje analizado y protegido por Telefonica Empresas * * 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/ Privileged, confidential or patient identifiable information may be contained in this message. This information is meant only for the use of the intended recipients. If you are not the intended recipient, or if the message has been addressed to you in error, do not read, disclose, reproduce, distribute, disseminate or otherwise use this transmission. Instead, please notify the sender by reply e-mail, and then destroy all copies of the message and any attachments. * * 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/

**References**:**st: Help with loops***From:*Rijo John <rmjohn@gmail.com>

- Prev by Date:
**st: Problems with STATA 10, e.g. saving file causes variables to dropout,** - Next by Date:
**Re: Re: st: Is there an ideal do file editor?** - Previous by thread:
**Re: st: RE: Help with loops** - Next by thread:
**st: Help with loops** - Index(es):

© Copyright 1996–2016 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |