Fix 'variable z_ already defined' error in loop
up vote
1
down vote
favorite
My code in Stata initially opens each file using a foreach
loop that imports the files while erasing the first line for each file (not shown here).
Once this part is done, my 901
files have the following structure:
v1 v2 v3 v4 v5 ...
id 798 578 948 421
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
...
Then, I use this nested foreach
loop, which was provided as an answer to a previous question of mine, to rename variables:
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
tostring v`i', replace
local varname = strtoname(v`i'[1])
rename v`i' z`varname'
}
However, I get this error message:
'variable z_ already defined'.
I ran a set trace on
command to make sure that the problem is with the rename
command (because z_
cannot already be defined). The problem is indeed with this command. The same code worked perfectly fine before with another group of files so I don't understand.
I want my code to take the first line, to add z_
as a prefix to the number on the first line and to rename my variables (zid
for v1
, z_798
for v3
, z_578
for v3
and so on).
EDIT:
It seems that the problem would be in one specific file:
v1 v2 v3 v4 v5 …
Id 576 . 568 . …
2000 5958 937 949 161
2001 8471 1059 767 234
…
v3
would become z_
and v5
would become also z_
but it cannot since ‘variable z_ already defined’. It seems that some id
numbers are missing.
Any suggestions to fix the code/data?
stata
add a comment |
up vote
1
down vote
favorite
My code in Stata initially opens each file using a foreach
loop that imports the files while erasing the first line for each file (not shown here).
Once this part is done, my 901
files have the following structure:
v1 v2 v3 v4 v5 ...
id 798 578 948 421
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
...
Then, I use this nested foreach
loop, which was provided as an answer to a previous question of mine, to rename variables:
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
tostring v`i', replace
local varname = strtoname(v`i'[1])
rename v`i' z`varname'
}
However, I get this error message:
'variable z_ already defined'.
I ran a set trace on
command to make sure that the problem is with the rename
command (because z_
cannot already be defined). The problem is indeed with this command. The same code worked perfectly fine before with another group of files so I don't understand.
I want my code to take the first line, to add z_
as a prefix to the number on the first line and to rename my variables (zid
for v1
, z_798
for v3
, z_578
for v3
and so on).
EDIT:
It seems that the problem would be in one specific file:
v1 v2 v3 v4 v5 …
Id 576 . 568 . …
2000 5958 937 949 161
2001 8471 1059 767 234
…
v3
would become z_
and v5
would become also z_
but it cannot since ‘variable z_ already defined’. It seems that some id
numbers are missing.
Any suggestions to fix the code/data?
stata
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
My code in Stata initially opens each file using a foreach
loop that imports the files while erasing the first line for each file (not shown here).
Once this part is done, my 901
files have the following structure:
v1 v2 v3 v4 v5 ...
id 798 578 948 421
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
...
Then, I use this nested foreach
loop, which was provided as an answer to a previous question of mine, to rename variables:
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
tostring v`i', replace
local varname = strtoname(v`i'[1])
rename v`i' z`varname'
}
However, I get this error message:
'variable z_ already defined'.
I ran a set trace on
command to make sure that the problem is with the rename
command (because z_
cannot already be defined). The problem is indeed with this command. The same code worked perfectly fine before with another group of files so I don't understand.
I want my code to take the first line, to add z_
as a prefix to the number on the first line and to rename my variables (zid
for v1
, z_798
for v3
, z_578
for v3
and so on).
EDIT:
It seems that the problem would be in one specific file:
v1 v2 v3 v4 v5 …
Id 576 . 568 . …
2000 5958 937 949 161
2001 8471 1059 767 234
…
v3
would become z_
and v5
would become also z_
but it cannot since ‘variable z_ already defined’. It seems that some id
numbers are missing.
Any suggestions to fix the code/data?
stata
My code in Stata initially opens each file using a foreach
loop that imports the files while erasing the first line for each file (not shown here).
Once this part is done, my 901
files have the following structure:
v1 v2 v3 v4 v5 ...
id 798 578 948 421
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
...
Then, I use this nested foreach
loop, which was provided as an answer to a previous question of mine, to rename variables:
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
tostring v`i', replace
local varname = strtoname(v`i'[1])
rename v`i' z`varname'
}
However, I get this error message:
'variable z_ already defined'.
I ran a set trace on
command to make sure that the problem is with the rename
command (because z_
cannot already be defined). The problem is indeed with this command. The same code worked perfectly fine before with another group of files so I don't understand.
I want my code to take the first line, to add z_
as a prefix to the number on the first line and to rename my variables (zid
for v1
, z_798
for v3
, z_578
for v3
and so on).
EDIT:
It seems that the problem would be in one specific file:
v1 v2 v3 v4 v5 …
Id 576 . 568 . …
2000 5958 937 949 161
2001 8471 1059 767 234
…
v3
would become z_
and v5
would become also z_
but it cannot since ‘variable z_ already defined’. It seems that some id
numbers are missing.
Any suggestions to fix the code/data?
stata
stata
edited Nov 20 at 0:40
Pearly Spencer
9,198163352
9,198163352
asked Nov 19 at 23:17
windyboo
315
315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
When you have missing values you can do the following:
clear
input str10(v1 v2 v3 v4 v5)
id 798 . 948 .
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
end
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
local varname = strtoname(v`i'[1])
if "`varname'" == "_" local varname `i'
rename v`i' z`varname'
}
list
+------------------------------------+
| zid z_798 z3 z_948 z5 |
|------------------------------------|
1. | id 798 . 948 . |
2. | 2000 4567 8673 4806 9405 |
3. | 2001 5867 9001 5893 8790 |
4. | 2002 6878 9233 5712 7678 |
+------------------------------------+
Essentially, you replace the missing value with the value from the counter i
.
You answers are always helpful and appreciated!
– windyboo
Nov 20 at 1:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
When you have missing values you can do the following:
clear
input str10(v1 v2 v3 v4 v5)
id 798 . 948 .
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
end
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
local varname = strtoname(v`i'[1])
if "`varname'" == "_" local varname `i'
rename v`i' z`varname'
}
list
+------------------------------------+
| zid z_798 z3 z_948 z5 |
|------------------------------------|
1. | id 798 . 948 . |
2. | 2000 4567 8673 4806 9405 |
3. | 2001 5867 9001 5893 8790 |
4. | 2002 6878 9233 5712 7678 |
+------------------------------------+
Essentially, you replace the missing value with the value from the counter i
.
You answers are always helpful and appreciated!
– windyboo
Nov 20 at 1:07
add a comment |
up vote
1
down vote
accepted
When you have missing values you can do the following:
clear
input str10(v1 v2 v3 v4 v5)
id 798 . 948 .
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
end
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
local varname = strtoname(v`i'[1])
if "`varname'" == "_" local varname `i'
rename v`i' z`varname'
}
list
+------------------------------------+
| zid z_798 z3 z_948 z5 |
|------------------------------------|
1. | id 798 . 948 . |
2. | 2000 4567 8673 4806 9405 |
3. | 2001 5867 9001 5893 8790 |
4. | 2002 6878 9233 5712 7678 |
+------------------------------------+
Essentially, you replace the missing value with the value from the counter i
.
You answers are always helpful and appreciated!
– windyboo
Nov 20 at 1:07
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
When you have missing values you can do the following:
clear
input str10(v1 v2 v3 v4 v5)
id 798 . 948 .
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
end
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
local varname = strtoname(v`i'[1])
if "`varname'" == "_" local varname `i'
rename v`i' z`varname'
}
list
+------------------------------------+
| zid z_798 z3 z_948 z5 |
|------------------------------------|
1. | id 798 . 948 . |
2. | 2000 4567 8673 4806 9405 |
3. | 2001 5867 9001 5893 8790 |
4. | 2002 6878 9233 5712 7678 |
+------------------------------------+
Essentially, you replace the missing value with the value from the counter i
.
When you have missing values you can do the following:
clear
input str10(v1 v2 v3 v4 v5)
id 798 . 948 .
2000 4567 8673 4806 9405
2001 5867 9001 5893 8790
2002 6878 9233 5712 7678
end
ds v*
local i 0
foreach var in `r(varlist)' {
local ++i
local varname = strtoname(v`i'[1])
if "`varname'" == "_" local varname `i'
rename v`i' z`varname'
}
list
+------------------------------------+
| zid z_798 z3 z_948 z5 |
|------------------------------------|
1. | id 798 . 948 . |
2. | 2000 4567 8673 4806 9405 |
3. | 2001 5867 9001 5893 8790 |
4. | 2002 6878 9233 5712 7678 |
+------------------------------------+
Essentially, you replace the missing value with the value from the counter i
.
edited Nov 20 at 0:41
answered Nov 20 at 0:27
Pearly Spencer
9,198163352
9,198163352
You answers are always helpful and appreciated!
– windyboo
Nov 20 at 1:07
add a comment |
You answers are always helpful and appreciated!
– windyboo
Nov 20 at 1:07
You answers are always helpful and appreciated!
– windyboo
Nov 20 at 1:07
You answers are always helpful and appreciated!
– windyboo
Nov 20 at 1:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384010%2ffix-variable-z-already-defined-error-in-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown