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: Batch renaming variables


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Batch renaming variables
Date   Thu, 18 Oct 2012 16:47:33 +0200

The one thing that may be a bit tricky is that the Mata function
numtobase26() is undocumentend. It basically maps positive integers to
letters such that 1=A, 2=B, ..., 26=Z, 27=AA, 28=AB, ..., 52=AZ,
53=BA, 54=BB, etc.

-- Maarten

On Thu, Oct 18, 2012 at 4:39 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> Maarten and I really used the same method, just in part implemented it
> differently.
>
> Your problem is easy to understand, but awkward to solve without
> knowing about loops and some macro manipulations.
>
> On Thu, Oct 18, 2012 at 3:34 PM, Wameq Raza <wameq.r@gmail.com> wrote:
>> Dear Maarten and Nick,
>> Thank you very much and I'll certain discuss the problem from the
>> outset! The method laid out
>> by Maarten worked like a charm! I'll have to spend a little more time
>> understanding the syntax
>> better, but my problem for now is definitely solved.
>> Many thanks once again,
>> Best
>> Wameq
>>
>> On Thu, Oct 18, 2012 at 4:28 PM, Maarten Buis <maartenlbuis@gmail.com> wrote:
>>> On Thu, Oct 18, 2012 at 4:03 PM, Wameq Raza wrote:
>>>> 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.
>>>
>>> *--------------------------- begin example --------------------------
>>> // create some example data
>>> clear
>>> set obs 10
>>> forvalues i = 915/1005 {
>>>         gen v`:display %04.0f `i'' = runiform()
>>> }
>>>
>>> // start renaming
>>> local j = 0
>>> forvalues i = 917(5)1005 {
>>>         local j = `j' + 1
>>>         mata: st_local("letter", strlower(numtobase26(strtoreal(st_local("j")))))
>>>         rename v`:display %04.0f `i'' cost_`letter'
>>> }
>>> *---------------------------- end example ---------------------------
>>>
> *
> *   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/



-- 
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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