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

From |
Steven Samuels <sjhsamuels@earthlink.net> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: loop to rename vars |

Date |
Fri, 10 Oct 2008 15:33:11 -0400 |

Sorry, -Sergiy- for my last remark on Statalist-it was uncalled for. -Steve On Oct 10, 2008, at 3:23 PM, Sergiy Radyakin wrote:

Does not seem to work: //----------------------------------------------------- clear sysuse auto renvars \ var1-var`c(k)' \ invalid name r(198); renvars var1-var`c(k)' r(111); renvars * var1-var`c(k)' variable var1 not found r(111); which renvars c:\ado\plus\r\renvars.ado *! 2.2.1 15aug2000 Jeroen Weesie/ICS version version 9.2 *************************************************************Perhaps I am doing something wrong?? Is slash required? Did youmean -renamev- ?From the source code it follows that (at least sometimes) it may stop with a message di in re "`oldname' could not be renamed to `newname'" Is this what you mean will happen? It should never occur in the case in question because vars may be sequentially numbered, and 32-character names allow you to allocate more names than the Stata limit. My preferred strategy would be to: 1. generate K temporary variable names (note that it is Stata's responsibility to ensure that the suggested variable name is unique) 2. rename each variable to the temporary name 3. rename each variable to the sequential name program rename_seq forvalues i=1/`=c(k)' { tempvar tmp`i' } ds local i = 1 foreach var of varlist `r(varlist)' { rename `var' `tmp`i'' local ++i } ds local i = 1 foreach var of varlist `r(varlist)' { rename `var' var`i' local ++i } end /* any resemblance to the code of Friedrich is purely coincidential */ Regards, S.R.On Fri, Oct 10, 2008 at 2:46 PM, Nick Cox <n.j.cox@durham.ac.uk>wrote:You can try it and see how smart the authors were (on this point). Nick n.j.cox@durham.ac.uk Sergiy RadyakinOn Fri, Oct 10, 2008 at 2:26 PM, Nick Cox <n.j.cox@durham.ac.uk>wrote:You don't need to fire up -ds- beforehand. This can thus bereduced tolocal i = 1 foreach var of var * { ren `var' var`i' local ++i } In fact you don't need a loop. With -renvars- from the Stata Journal (-search renvars- for location) you can go renvars \ var1-var`c(k)'What if var5 already exists (and it is 10th in order). Will -renvars-be intelligent enough to sense it and adjust numberingaccordingly? orwill it just crash with error 110 after renaming the fifth variable?c(k) is held by Stata as the number of variables -- see results of -creturn li- and it has a local macro persona `c(k)'. Naturally -renvars- is doing the loop on your behalf. It also checksforname problems. If Mai does not want all variables renamed, then the advice ismodifiedaccordingly. Nick n.j.cox@durham.ac.uk Friedrich Huebler ds local i = 1 foreach var of varlist `r(varlist)' { ren `var' var`i' local ++i } On Thu, Oct 9, 2008 at 11:31 PM, mai7777 <mai7777@gmail.com> wrote:How can I loop over vars to rename them by their order number. so they become var1, var2, var3...?* * 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/

**Follow-Ups**:**Re: st: loop to rename vars***From:*"Sergiy Radyakin" <serjradyakin@gmail.com>

**References**:**st: loop to rename vars***From:*mai7777 <mai7777@gmail.com>

**Re: st: loop to rename vars***From:*"Friedrich Huebler" <fhuebler@gmail.com>

**RE: st: loop to rename vars***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**Re: st: loop to rename vars***From:*"Sergiy Radyakin" <serjradyakin@gmail.com>

**RE: st: loop to rename vars***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**Re: st: loop to rename vars***From:*"Sergiy Radyakin" <serjradyakin@gmail.com>

- Prev by Date:
**Re: st: loop to rename vars** - Next by Date:
**Re: st: forvalues and _N** - Previous by thread:
**Re: st: loop to rename vars** - Next by thread:
**Re: st: loop to rename vars** - Index(es):

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