Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

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

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

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

Subject |
RE: st: RE: Foreach loop for subsequent variables |

Date |
Tue, 1 Nov 2011 16:49:44 +0000 |

As Matthew is implying, 1 2 3 4 ... which are the results of evaluating `i' `j' cannot be variable names, so -keep- will indeed just give you an error with your existing code. Matthew's code should work. This is an alternative: unab varlist : * tokenize `varlist' forvalues i = 1(2)897 { preserve local j = `i' + 1 keep ``i'' ``j'' save `i'2 restore } But there is no important reason to use this code rather than Matthew's. Nick n.j.cox@durham.ac.uk Matthew White Not sure this is the best way, but how about: ***BEGIN*** ds loc varl `r(varlist)' forvalues i = 1(2)897 { preserve local j = `i' +1 keep `:word `i' of `varl'' `:word `j' of `varl'' save `i'2 restore } ***END*** On Tue, Nov 1, 2011 at 12:32 PM, Dmitriy Glumov <glumovdm@gmail.com> wrote: > Sorry for not including the code in the previous message, here it is: > > forvalues i = 1(2)897 { > preserve > local j = `i' +1 > keep `i' `j' > save `i'2 > restore > } > > And Stata's error message: > > 1 invalid name > r(198); > > end of do-file > > I am trying to save variables 1 and 2 together as a separate file, > variables 3 and 4 together as another separate file, and so on. This > process can be done by hand but it would be very time-consuming (there > are 898 variables) - and, that's why I am trying to write a loop for > Stata do it automatically. If naming is an issue, all variables have > different names (they are not named var1, var2, etc, but are rather > named using first 4 letters of a company name). Again, thank you for > the help and if anyone can share a solution or approach that is > appropriate for this situation, it would be greatly appreciated. > > Dmitriy > > On Tue, Nov 1, 2011 at 11:54 AM, Nick Cox <n.j.cox@durham.ac.uk> wrote: >> I can't see your code from here. I guess you are confusing i (letter i) with 1 (number 1). If that is not the answer, you need to show us your code. >> >> Nick >> n.j.cox@durham.ac.uk >> >> Dmitriy Glumov >> >> Thank you for your response. When I run forval as you suggested, I get >> the error message (r 198) saying that "1" is an invalid name (I >> believe this message refers to the very first 1 in the program). I am >> not exactly sure why this message appears (if it helps, I work with >> Stata 11), as other online examples that use the same context appear >> to work. Again, thank you for your help and it would be greatly >> appreciated if you or anyone else can help with this error. >> >> On Tue, Nov 1, 2011 at 10:56 AM, Nick Cox <n.j.cox@durham.ac.uk> wrote: >> >>> Let's suppose your variables run var1 ... var42 so that the last pair is var41-var42. >>> >>> Then first I would use -forvalues- here. It is easier and a smidgen faster. >>> >>> forval i = 1(2)41 { >>> >>> local j = `i' + 1 >>> <whatever> var`i' var`j' >>> >>> } >>> >>> You can also go >>> >>> forval i = 1(2)41 { >>> >>> <whatever> var`i' var`=`i'+1' >>> >>> } >>> >>> See [U] 18.3.8 and -help macro-. >>> >>> Nick >>> n.j.cox@durham.ac.uk >>> >>> Dmitriy Glumov >>> >>> I have recently begun to use Stata and have run into the problem >>> writing a foreach loop. I have a large dataset of variables, paired in >>> a way that var1 and var2 are together, var3 and var4 are together, and >>> so on. I am writing a foreach loop for each individual odd variable >>> and, everytime the program loops, I need to include a subsequent even >>> variable as well. So, for example, given that var1 is `x', I need to >>> include var2 into the program, too (in essence, `x' + 1). The problem >>> doesn't seem to be very difficult, and I am sure there is a simple >>> solution to it, but I just can't find it anywhere. Thank you for your >>> consideration, any help would be greatly appreciated! >> * * 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/

**References**:**st: Foreach loop for subsequent variables***From:*Dmitriy Glumov <glumovdm@gmail.com>

**st: RE: Foreach loop for subsequent variables***From:*Nick Cox <n.j.cox@durham.ac.uk>

**Re: st: RE: Foreach loop for subsequent variables***From:*Dmitriy Glumov <glumovdm@gmail.com>

**RE: st: RE: Foreach loop for subsequent variables***From:*Nick Cox <n.j.cox@durham.ac.uk>

**Re: st: RE: Foreach loop for subsequent variables***From:*Dmitriy Glumov <glumovdm@gmail.com>

**Re: st: RE: Foreach loop for subsequent variables***From:*Matthew White <mwhite@poverty-action.org>

- Prev by Date:
**Re: st: RE: Foreach loop for subsequent variables** - Next by Date:
**st: RE: query on testing uniform distributions** - Previous by thread:
**Re: st: RE: Foreach loop for subsequent variables** - Next by thread:
**st: identification through heteroskedasticity** - Index(es):