Fix 'variable z_ already defined' error in loop
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 |
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 |
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 '18 at 0:40
Pearly Spencer
10.9k173461
10.9k173461
asked Nov 19 '18 at 23:17
windyboowindyboo
315
315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 at 1:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 1:07
add a comment |
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 '18 at 1:07
add a comment |
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 '18 at 0:41
answered Nov 20 '18 at 0:27
Pearly SpencerPearly Spencer
10.9k173461
10.9k173461
You answers are always helpful and appreciated!
– windyboo
Nov 20 '18 at 1:07
add a comment |
You answers are always helpful and appreciated!
– windyboo
Nov 20 '18 at 1:07
You answers are always helpful and appreciated!
– windyboo
Nov 20 '18 at 1:07
You answers are always helpful and appreciated!
– windyboo
Nov 20 '18 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.
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