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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 0:40









      Pearly Spencer

      9,198163352




      9,198163352










      asked Nov 19 at 23:17









      windyboo

      315




      315
























          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.






          share|improve this answer























          • You answers are always helpful and appreciated!
            – windyboo
            Nov 20 at 1:07











          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',
          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
          });


          }
          });














          draft saved

          draft discarded


















          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








          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.






          share|improve this answer























          • You answers are always helpful and appreciated!
            – windyboo
            Nov 20 at 1:07















          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.






          share|improve this answer























          • You answers are always helpful and appreciated!
            – windyboo
            Nov 20 at 1:07













          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python