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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Renaming variables using foreach


From   Suryadipta Roy <sroy2138@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Renaming variables using foreach
Date   Tue, 26 Jun 2012 18:49:41 -0400

Dear Nick,

I am using Stata version 11.2. Actually, the variable names that
result from cpying the .csv file into Stata contain a lot of
characters. Thus, e.g. the first variable is "Agricultural raw
materials exports (% of merchandise exports)" which when copied into
Stata reads as  "Agricultural_raw_materials_expor", and so on. I
wanted to label these variables with their existing variable names,
and then change the variable names into something meaningful. In the
process, I was trying to use -foreach- in order to make my life
easier.

Actually, Steve has suggested me the loop that would label the
variables with their "variable" names, and all my problems have been
solved.

Thank you and Steve once again for all your guidance and help.
Suryadipta.

On Tue, Jun 26, 2012 at 6:32 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> On the first para, I think I see what you mean: they are too long to
> be practical.
>
> Nick
>
> On Tue, Jun 26, 2012 at 11:22 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>> This is contradictory. If the variable names in some other software
>> are too long to be handled in Stata, how can they renamed in Stata?
>> You can't even import them. Conversely, names imported in Stata, while
>> possibly truncated, do not need to renamed.
>>
>> The FAQ spells out
>>
>> The current version of Stata is 12.1. Please specify if you are using
>> an earlier version; otherwise, the answer to your question is likely
>> to refer to commands or features unavailable to you.
>>
>> See also http://www.stata.com/statalist/archive/2012-05/msg00998.html
>> for reminders of what else you should know.
>>
>> Nick
>>
>> On Tue, Jun 26, 2012 at 11:08 PM, Suryadipta Roy <sroy2138@gmail.com> wrote:
>>> Dear Nick and Steve,
>>>
>>> Thank you very much the help! Actually, I forgot to mention that I am
>>> using Stata 11.2, and what I really wanted to do was to "label" the
>>> variables with the existing variable names (since the current variable
>>> names from the World Develepment Indicators are too long). The idea
>>> was thus to change the long names to var1, var2, etc, and then use
>>> -label- . I would now check if I can somehow manage to get the
>>> variables labelled by their variable names (obviously I can do that
>>> manually as well) in Stata 11.
>>>
>>> And, Nick's solution works and will read into it very carefully.
>>>
>>> Thanks again!
>>> Suryadipta.
>>>
>>> On Tue, Jun 26, 2012 at 5:47 PM, Steve Nakoneshny <scnakone@ucalgary.ca> wrote:
>>>> Suryadipta,
>>>>
>>>> Your loop fails as you are asking for each variable in your varlist to be renamed var1. Assuming you're using Stata 12, you don't need a loop to solve this issue. You can achieve your desired result (or some reasonable facsimile) using -rename-:
>>>>
>>>> rename (Agricultural_raw_materials_expor - Value_lost_due_to_electrical_ou) var#, addnumber
>>>>
>>>> See -help rename- for more details.
>>>>
>>>> Steve
>>>>
>>>>
>>>> On 2012-06-26, at 3:20 PM, Suryadipta Roy wrote:
>>>>
>>>>> Dear Statalisters,
>>>>>
>>>>> I am currently learning looping in Stata and is stuck with the
>>>>> following problem: I have about 150 variables that I want to rename as
>>>>> var1 var2, etc.. upto var 150. I tried the following:
>>>>>
>>>>> local i = `i' + 1
>>>>> foreach x of varlist  Agricultural_raw_materials_expor -
>>>>> Value_lost_due_to_electrical_out  {
>>>>> 2. rename `x' var`i'
>>>>> 3. }
>>>>>
>>>>> I am getting the following error message:
>>>>> var1 already defined
>>>>> r(110);
>>>>>
>>>>> I find that only the first variable is renamed as "var1" and then the
>>>>> loop stopped running. Any help is greatly appreciated.
>
> *
> *   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/
*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index