Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Is it possible to use --rename-- with the renumber option to rename variables in reverse order?


From   Phil Clayton <philclayton@internode.on.net>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Is it possible to use --rename-- with the renumber option to rename variables in reverse order?
Date   Sun, 28 Oct 2012 16:31:59 +1100

foreach n of numlist 2/11 {
    rename v`n' v`=2011 - `n''
}

On 28/10/2012, at 15:16, Aaron Kirkman <ak1795mailserv@gmail.com> wrote:

> Is there a simpler way to do this in a loop? (I realise this solution
> is fairly simple, but I want to make sure I didn't miss something).
> 
> ****
> clear
> input v2 v3 v4 v5 v6 v7 v8 v9 v10 v11
> 309 418 333 153 259 172 359 319 440 317
> end
> 
> foreach v of varlist v2-v11 {
>    local year = 2011 - real(subinstr("`v'", "v", "", .))
>    rename `v' v`year'
> }
> ****
> 
> Aaron
> 
> On Sat, Oct 27, 2012 at 10:49 PM, kantor.d@att.net <kantor.d@att.net> wrote:
>> I don't know if you can do that within the capabilities of -rename- alone, but it can be done in a loop.
>> 
>> Sent with Verizon Mobile Email
>> 
>> 
>> ---Original Message---
>> From: statalist@hsphsun2.harvard.edu
>> Sent: 10/27/2012 9:38 pm
>> To: statalist@hsphsun2.harvard.edu
>> Subject: st: Is it possible to use --rename-- with the renumber option to rename variables in reverse order?
>> 
>> Dear Statalist,
>> 
>> I have a dataset with variables v2 through v11, in that order. I'd
>> like to rename the variables v2009, v2008, v2007, etc. down to v2000.
>> In other words, v2 = v2009, v3 = v2008, etc. Is there a way to do this
>> with the --rename-- command in Stata 12.1 ? Perhaps some variation on:
>> 
>> ****
>> rename v* v#, renumber(2009)
>> ****
>> 
>> which gives me v2 = 2009, v3 = 2010, etc. In essence, I'm trying to
>> use a similar behaviour but in reverse order. I know that reversing
>> the order of the data set and using --rename-- in a similar fashion
>> will work:
>> 
>> ****
>> order v11 v10 v9 v8 v7 v6 v5 v4 v3 v2
>> rename v* v#, renumber(2000)
>> ****
>> 
>> I'm curious if there is a more general way, since I don't think I can
>> specify a varlist in reverse order, i.e. v11 - v2 will not work unless
>> the variables are already in that format.
>> 
>> Thank you,
>> 
>> Aaron Kirkman
>> *
>> *   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/
>> 
>> *
>> *   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/
> *
> *   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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index