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: renaming variables from first observation
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: renaming variables from first observation
Date
Thu, 25 Aug 2011 10:13:17 +0100
Let's assume that your first values are all legal names. You could
indeed do this
forval j = 1/14 {
local names `names' `=var`j'[1]'
}
renvars var1-var14 \ `names'
But that's actually dozens of lines more code (look _inside_ -renvars-) than
forval j = 1/14 {
rename var`j' `=var`j'[1]'
}
and the second is in any case shorter and more direct, even without
looking inside -renvars-.
You asked what a solution was with -renvars- and I agree that it looks
complicated. The -map()- option was mine and intended as an outlet for
the desperate user-programmer whose problem was not met by any of the
other options. But usually you need to be moderately fluent in Stata
to make effective use of it. The program was written for the authors'
use and any utility beyond that is a pleasant side-effect!
`= exp'
evaluates an expression on the fly and is documented tersely at -help
macro-. But the main idea is simple. Consider as part of a command
line
`= 2 + 2'
Stata sees ` ' and so expects something like a macro reference to be
substituted. But instead of a macro name there is
= 2 + 2
Stata does the calculation on the fly. In this case the result is 4
and that is what the command would see.
-renvars- is over a decade old now. After that length of time
StataCorp caught up and the official -rename- is now much versatile in
Stata 12, although I doubt it has a way to solve your problem without
a loop. I would be happy to be shown wrong on that.
Nick
On Thu, Aug 25, 2011 at 9:50 AM, Abhimanyu Arora
<[email protected]> wrote:
> Yes Nick, precisely, my values in the first observation were not
> legal. What I meant by storing values in a macro was something like a
> horizontal -levelsof- command. Once I obtain the values in a single
> macro I thought of simply using your and Jeroen Weesie's -renvars-.
>
> But the map option seems not that easy to understand, perhaps because
> I find `quotes' confusing sometimes. One needs to have a string
> expression in the parentheses while using the map option but I see
> that the expression in your example is in `quotes', normally used for
> macros. Would be really great if you could clarify this fundamental
> issue.
>
> Many thanks
> Abhimanyu
>
>
> . list in 1
>
> +--------------------------------------------------------------------------------------------------------------------+
> 1. | var1 | var2 | var3 | var4 | var5
> | var6 | var7 | var8 | var9 | var10 | var11 | var12 |
> | Series Code | Series Name | Country Code | Country Name | 2000
> | 2001 | 2002 | 2003 | 2004 | 2005 | 2006 | 2007 |
> |---------------------------------------------------------+----------------------------------------------------------|
> | var13 |
> var14 |
> | 2008 |
> 2009 |
> +--------------------------------------------------------------------------------------------------------------------+
>
> On Thu, Aug 25, 2011 at 10:35 AM, Nick Cox <[email protected]> wrote:
>> Already answered, but not correctly. You could e.g. use the first word
>> of the first value. This works:
>>
>> . l
>>
>> +------------------------------+
>> | var1 var2 var3 |
>> |------------------------------|
>> 1. | Frog Toad Venomous snake |
>> 2. | 1 2 3 |
>> +------------------------------+
>>
>> . renvars , map(`=word("@", 1)')
>>
>> . l
>>
>> +------------------------------+
>> | Frog Toad Venomous |
>> |------------------------------|
>> 1. | Frog Toad Venomous snake |
>> 2. | 1 2 3 |
>> +------------------------------+
>>
>> Nick
>>
>> On Thu, Aug 25, 2011 at 9:25 AM, Abhimanyu Arora
>> <[email protected]> wrote:
>>> I caught the mistake, thanks to -set trace on-. 'Code' is the second
>>> word of my observation of the first variable and of course a variable
>>> name has to be single worded. But my question on possibility of using
>>> Nick's -renvars- remains.
>>> Best regards
>>> Abhimanyu
>>>
>>>
>>> On Thu, Aug 25, 2011 at 10:07 AM, Abhimanyu Arora
>>> <[email protected]> wrote:
>>>> Good morning statalist
>>>> I would like to rename my variables var1-var14 to the corresponding
>>>> values from the first observation.
>>>> I followed the post on
>>>> http://www.stata.com/statalist/archive/2004-07/msg00009.html and
>>>> modified it for my purpose.
>>>>
>>>> but I got this error
>>>>
>>>> . forvalues k = 1/14 {
>>>> 2. local newname = var`k'[1]
>>>> 3. ren var`k' `newname'
>>>> 4. }
>>>>
>>>> Code not allowed
>>>>
>>>> Also is it posible to store the values of an observation in a macro?
>>>> Perhaps I could then use Nick Cox's -renvars-?
>>>>
*
* 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/