Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Batch renaming variables


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Batch renaming variables
Date   Thu, 18 Oct 2012 15:17:25 +0100

1. If you don't state your real problem at the outset, you run the
risk of wasting everyone's time.

2. -renvars- is from the Stata Journal. Please read the FAQ to see why
you are asked to explain this.

3. The new -rename- (Stata 12) is much more flexible than the old.

4. Try

* copy built-in alphabet
local alpha "`c(alpha)'"
local i = 1
forval j = 917(5)1002 {
     local letter : word `i' of `alpha'
* leading 0 needs to be inserted if < 1000
     local suffix : di %04.0f `j'
     rename var`suffix'  cost_`letter'
     local ++i
}


On Thu, Oct 18, 2012 at 3:03 PM, Wameq Raza <[email protected]> wrote:
> Hi again David,
> As I mentioned, I'm not sure I understand this syntax enough to be
> able to manipulate it and was wondering if you could kindly help me a
> little more. Here's my exact problem:
>
> The variables I have start from v0915 and continue to v1005 and I need
> to rename every 5th variable in this list starting from v0917. The
> variables to be renamed to are: cost_a, cost_b....cost_n.
>
> How would I modify the syntax you sent me for this scenario?
> Best
> Wameq
>
> On Thu, Oct 18, 2012 at 3:50 PM, David Kantor <[email protected]> wrote:
>> At 09:36 AM 10/18/2012, Wameq Raza wrote:
>>>
>>> Hi everyone,
>>> I'm having some trouble trying to figure out how to do batch renames.
>>> To elaborate, suppose I have 10 variables: v1, v2....v10 and I want to
>>> rename every second var (ie v2, v4, v6, v8 and v10) to cost_a,
>>> cost_b....cost_e. Can anyone please help me figure this out? I've
>>> tried looking into renvars, but can't seem to figure it out.
>>
>>
>> If there are only five renames, I would just write them out.
>> But if you want a programmatic solution (as maybe there are really many more
>> renames)...
>> (untested)
>>
>> local suffixes "a b c d e"
>>
>> forvalues jj = 1/5 {
>>         ren v`=`jj'*2' cost_`=word("`suffixes'", `jj')'
>> }
>>
>> ----
>> Another way of getting the suffixes (a, b, c,...) is to use
>> char(96+ `jj')
>> but you need to be careful not to exceed jj=26.
>> But that's true in any case.
>>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index