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   Wameq Raza <[email protected]>
To   [email protected]
Subject   Re: st: Batch renaming variables
Date   Thu, 18 Oct 2012 16:03:33 +0200

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.
>
> HTH
> --David
>
>
> *
> *   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/



-- 
W A M E Q   R A Z A
*
*   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