Hello,
I have some variables of a certain format (x_AAUU - where AA = 2 digit year, UU = 2 digit week), which I need to perform some expressions on in order to rename them. Here is the code:
input x_9801 x_9925 x_0050 x_0351 x1311
1 2 3 4 5
end
foreach var of varlist x_* {
local aa=substr(string(`var'),3,2)
local uu=substr(string(`var'),5,2)
ren `var' "x_19"+"`aa'"+"`uu'" if `aa' > 90 & `aa' <= 99
ren `var' "x_20"+"`aa'"+"`uu'" if `aa' < 90
}
when i set trace on, Stata reveals that the above code basically does not properly set the aa or uu macro, so when it renames the vars, they just show as blank.
how can I get it to properly set the macro value so it can be used to rename the variables?
Edit: I have tried additional code and I really can't see why i keep getting rename syntax errors:
input x_9801 x_9925 x_0050 x_0351 x1311
1 2 3 4 5
end
foreach var of varlist x_* {
local aa=substr(string(`var'),3,2)
local uu=substr(string(`var'),5,2)
local aan "x_19"+"`aa'"+"`uu'"
local aat "x_20"+"`aa'"+"`uu'"
ren `var' `aan' if `aa' > 90 & `aa' <= 99
ren `var' `aat' if `aa' < 90
}
The traceback then gives me syntax error for rename, but it says exactly:
= rename x_9801 x_199801 if 98 > 90 & 98 <= 99
syntax error
Syntax is
rename old name new name ...
....
EDIT2: Fixed, i messed up the format of renaming conditional:
foreach var of varlist x_* {
local aa=substr(string(`var'),3,2)
local uu=substr(string(`var'),5,2)
local aan "x_19"+"`aa'"+"`uu'"
local aat "x_20"+"`aa'"+"`uu'"
if `aa' > 90 & `aa' <= 99 ren `var' `aan'
if `aa' < 90 ren `var' `aat'
}
works fine.
I have some variables of a certain format (x_AAUU - where AA = 2 digit year, UU = 2 digit week), which I need to perform some expressions on in order to rename them. Here is the code:
input x_9801 x_9925 x_0050 x_0351 x1311
1 2 3 4 5
end
foreach var of varlist x_* {
local aa=substr(string(`var'),3,2)
local uu=substr(string(`var'),5,2)
ren `var' "x_19"+"`aa'"+"`uu'" if `aa' > 90 & `aa' <= 99
ren `var' "x_20"+"`aa'"+"`uu'" if `aa' < 90
}
when i set trace on, Stata reveals that the above code basically does not properly set the aa or uu macro, so when it renames the vars, they just show as blank.
how can I get it to properly set the macro value so it can be used to rename the variables?
Edit: I have tried additional code and I really can't see why i keep getting rename syntax errors:
input x_9801 x_9925 x_0050 x_0351 x1311
1 2 3 4 5
end
foreach var of varlist x_* {
local aa=substr(string(`var'),3,2)
local uu=substr(string(`var'),5,2)
local aan "x_19"+"`aa'"+"`uu'"
local aat "x_20"+"`aa'"+"`uu'"
ren `var' `aan' if `aa' > 90 & `aa' <= 99
ren `var' `aat' if `aa' < 90
}
The traceback then gives me syntax error for rename, but it says exactly:
= rename x_9801 x_199801 if 98 > 90 & 98 <= 99
syntax error
Syntax is
rename old name new name ...
....
EDIT2: Fixed, i messed up the format of renaming conditional:
foreach var of varlist x_* {
local aa=substr(string(`var'),3,2)
local uu=substr(string(`var'),5,2)
local aan "x_19"+"`aa'"+"`uu'"
local aat "x_20"+"`aa'"+"`uu'"
if `aa' > 90 & `aa' <= 99 ren `var' `aan'
if `aa' < 90 ren `var' `aat'
}
works fine.
Comment