Statalist


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

Re: st: loop to rename vars


From   "Sergiy Radyakin" <[email protected]>
To   [email protected]
Subject   Re: st: loop to rename vars
Date   Fri, 10 Oct 2008 14:43:09 -0400

On Fri, Oct 10, 2008 at 2:26 PM, Nick Cox <[email protected]> wrote:
> You don't need to fire up -ds- beforehand. This can thus be reduced to
>
> local 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 numbering accordingly? or
will it just crash with error 110 after renaming the fifth variable?

Thank you, Sergiy Radyakin

>
> 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 checks for
> name problems.
>
> If Mai does not want all variables renamed, then the advice is modified
> accordingly.
>
> Nick
> [email protected]
>
> 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 <[email protected]> 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/



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