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]

st: Renaming variables using foreach


From   Suryadipta Roy <[email protected]>
To   [email protected]
Subject   st: Renaming variables using foreach
Date   Tue, 26 Jun 2012 17:20:44 -0400

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.

Sincerely,
Suryadipta.
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index