I have a problem with rename using local inside loop.
I would like to import an excel file with some block with same structure in the same page.
I use a loop but appear an error when variables are renamed.
I run:
Code:
forvalues i = 5(30)275 {
local b = `i'+27
local combustibles "Petroleo GasNat CarbonMin Hidroenergia Geotermica Nuclear Leña CañaDeriv OtrasPrim tes"
import excel "Series de oferta y demanda (1).xlsx", cellrange(A`i':AZ`b') firstrow clear
rename A Country
foreach v of varlist B-AZ {
local x : variable label `v'
rename `v' `combustibles'`x'
}
reshape long `combustibles', i(Country) j(year)
save `combustibles', replace
}Then appear an error:
Code:
syntax error
Syntax is
rename oldname newname [, renumber[(#)] addnumber[(#)] sort ...]
rename (oldnames) (newnames) [, renumber[(#)] addnumber[(#)] sort ...]
rename oldnames , {upper|lower|proper}
r(198);It works if I used:
Code:
forvalues i = 5(30)275 {
local b = `i'+27
local combustibles "Petroleo GasNat CarbonMin Hidroenergia Geotermica Nuclear Leña CañaDeriv OtrasPrim tes"
import excel "Series de oferta y demanda (1).xlsx", cellrange(A`i':AZ`b') firstrow clear
rename A Country
foreach v of varlist B-AZ {
local x : variable label `v'
rename `v' c_`i'_`b'`x'
}
reshape long c_`i'_`b', i(Country) j(year)
save `combustibles', replace
}What can I do?
Thanks in advance,
Sebastián.
0 Response to error: rename using local inside loop
Post a Comment