How to use a forval loop and take into account one exception?










0















I have 900 files that include 1,000 variables named v1 – v1000 and 1 file that includes 43 variables named v1 – v43.



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).



Then, I use this nested forval loop to rename variables:



forval j = 1/1000 
local varname = strtoname(v`j'[1])
rename v`j' x`varname'



This code doesn’t work well because my last file doesn’t have the variables named v44 – v1000.



I need a code snippet that can read 1,000 variables for the first 900 files and 43 variables for the last file. I tried with c(k) (i.e. counting the number of variables in each file before looping):



forval j = 1/ `c(k)' 
local varname = strtoname(v`j'[1])
rename v`j' x`varname'



However, this doesn’t work. Any suggestions?










share|improve this question




























    0















    I have 900 files that include 1,000 variables named v1 – v1000 and 1 file that includes 43 variables named v1 – v43.



    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).



    Then, I use this nested forval loop to rename variables:



    forval j = 1/1000 
    local varname = strtoname(v`j'[1])
    rename v`j' x`varname'



    This code doesn’t work well because my last file doesn’t have the variables named v44 – v1000.



    I need a code snippet that can read 1,000 variables for the first 900 files and 43 variables for the last file. I tried with c(k) (i.e. counting the number of variables in each file before looping):



    forval j = 1/ `c(k)' 
    local varname = strtoname(v`j'[1])
    rename v`j' x`varname'



    However, this doesn’t work. Any suggestions?










    share|improve this question


























      0












      0








      0








      I have 900 files that include 1,000 variables named v1 – v1000 and 1 file that includes 43 variables named v1 – v43.



      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).



      Then, I use this nested forval loop to rename variables:



      forval j = 1/1000 
      local varname = strtoname(v`j'[1])
      rename v`j' x`varname'



      This code doesn’t work well because my last file doesn’t have the variables named v44 – v1000.



      I need a code snippet that can read 1,000 variables for the first 900 files and 43 variables for the last file. I tried with c(k) (i.e. counting the number of variables in each file before looping):



      forval j = 1/ `c(k)' 
      local varname = strtoname(v`j'[1])
      rename v`j' x`varname'



      However, this doesn’t work. Any suggestions?










      share|improve this question
















      I have 900 files that include 1,000 variables named v1 – v1000 and 1 file that includes 43 variables named v1 – v43.



      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).



      Then, I use this nested forval loop to rename variables:



      forval j = 1/1000 
      local varname = strtoname(v`j'[1])
      rename v`j' x`varname'



      This code doesn’t work well because my last file doesn’t have the variables named v44 – v1000.



      I need a code snippet that can read 1,000 variables for the first 900 files and 43 variables for the last file. I tried with c(k) (i.e. counting the number of variables in each file before looping):



      forval j = 1/ `c(k)' 
      local varname = strtoname(v`j'[1])
      rename v`j' x`varname'



      However, this doesn’t work. Any suggestions?







      stata






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 14:42









      JohnE

      14.2k53457




      14.2k53457










      asked Nov 13 '18 at 22:25









      windyboowindyboo

      315




      315






















          2 Answers
          2






          active

          oldest

          votes


















          3














          Using some artificially-generated datasets:



          forvalues i = 1 / 10 
          clear
          set obs 5
          forvalues j = 1 / 10
          generate v`j' = rnormal()

          save data`i', replace


          use data10, clear
          drop v5-v10
          save data10, replace
          clear


          Here's an example:



          local allfiles: dir . files "*.dta"

          foreach dta in `allfiles'
          use `dta', clear
          ds v*
          local i 0
          foreach var in `r(varlist)'
          local ++i
          rename `var' x`i'

          save `dta', replace



          Below you can see the results:



          use data1, clear
          list
          +-----------------------------------------------------------------------------------------------------------------------+
          | x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 |
          |-----------------------------------------------------------------------------------------------------------------------|
          1. | -.0658037 1.01091 .4984255 -.1489926 -.8711151 -2.013461 .2881269 .1096137 -1.400732 -.9703687 |
          2. | 2.389028 -1.537572 -.3862164 -.6072646 -2.262745 1.315605 1.686188 -.5404406 1.078409 -.117408 |
          3. | -.2777171 .6730747 -.5674241 -.578813 -.8116008 -.5623083 .7675297 -1.117687 -1.196418 .417776 |
          4. | 2.109452 -1.035937 2.063489 1.183948 -.5243855 -1.020852 -.8674071 -.3530601 -.1752301 1.556753 |
          5. | .7309901 -.6810378 -.9365283 1.818035 .0232499 2.533621 -.5896646 .5805199 .1430279 -1.926774 |
          +-----------------------------------------------------------------------------------------------------------------------+

          use data10, clear
          list

          +-----------------------------------------------+
          | x1 x2 x3 x4 |
          |-----------------------------------------------|
          1. | -.1145661 1.830756 1.860386 .3472159 |
          2. | 1.10806 -.5629539 1.028942 -.7665766 |
          3. | 1.269463 -1.433527 -.6405479 .8663427 |
          4. | .0158674 1.49529 2.840101 -.9815945 |
          5. | .0969952 -.0885036 -2.036327 -.2538646 |
          +-----------------------------------------------+





          share|improve this answer
































            1














            A very easy, not very elegant method would be to check whether the variable exists with capture:



            foreach dta in `allfiles' 
            use `dta', clear
            forval j = 1/1000
            local varname = strtoname(v`j'[1])
            cap confirm variable v`j'
            if (_rc == 0) rename v`j' x`varname'

            save `dta', replace






            share|improve this answer

























            • confirm variable exists for precisely this purpose.

              – Nick Cox
              Nov 14 '18 at 9:51











            • @NickCox not sure why you upvoted this. The loop will not work because there are no 1000 variables in 1 file but across 901 files.

              – Pearly Spencer
              Nov 14 '18 at 9:59











            • You're right, but it can be tweaked easily.

              – Nick Cox
              Nov 14 '18 at 10:02











            • I get _rc == 0 for an existing variable and 111 if the variable does not exist.

              – E. Sommer
              Nov 19 '18 at 10:53











            • you were right, sorry

              – E. Sommer
              Nov 19 '18 at 12:09










            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
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290438%2fhow-to-use-a-forval-loop-and-take-into-account-one-exception%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Using some artificially-generated datasets:



            forvalues i = 1 / 10 
            clear
            set obs 5
            forvalues j = 1 / 10
            generate v`j' = rnormal()

            save data`i', replace


            use data10, clear
            drop v5-v10
            save data10, replace
            clear


            Here's an example:



            local allfiles: dir . files "*.dta"

            foreach dta in `allfiles'
            use `dta', clear
            ds v*
            local i 0
            foreach var in `r(varlist)'
            local ++i
            rename `var' x`i'

            save `dta', replace



            Below you can see the results:



            use data1, clear
            list
            +-----------------------------------------------------------------------------------------------------------------------+
            | x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 |
            |-----------------------------------------------------------------------------------------------------------------------|
            1. | -.0658037 1.01091 .4984255 -.1489926 -.8711151 -2.013461 .2881269 .1096137 -1.400732 -.9703687 |
            2. | 2.389028 -1.537572 -.3862164 -.6072646 -2.262745 1.315605 1.686188 -.5404406 1.078409 -.117408 |
            3. | -.2777171 .6730747 -.5674241 -.578813 -.8116008 -.5623083 .7675297 -1.117687 -1.196418 .417776 |
            4. | 2.109452 -1.035937 2.063489 1.183948 -.5243855 -1.020852 -.8674071 -.3530601 -.1752301 1.556753 |
            5. | .7309901 -.6810378 -.9365283 1.818035 .0232499 2.533621 -.5896646 .5805199 .1430279 -1.926774 |
            +-----------------------------------------------------------------------------------------------------------------------+

            use data10, clear
            list

            +-----------------------------------------------+
            | x1 x2 x3 x4 |
            |-----------------------------------------------|
            1. | -.1145661 1.830756 1.860386 .3472159 |
            2. | 1.10806 -.5629539 1.028942 -.7665766 |
            3. | 1.269463 -1.433527 -.6405479 .8663427 |
            4. | .0158674 1.49529 2.840101 -.9815945 |
            5. | .0969952 -.0885036 -2.036327 -.2538646 |
            +-----------------------------------------------+





            share|improve this answer





























              3














              Using some artificially-generated datasets:



              forvalues i = 1 / 10 
              clear
              set obs 5
              forvalues j = 1 / 10
              generate v`j' = rnormal()

              save data`i', replace


              use data10, clear
              drop v5-v10
              save data10, replace
              clear


              Here's an example:



              local allfiles: dir . files "*.dta"

              foreach dta in `allfiles'
              use `dta', clear
              ds v*
              local i 0
              foreach var in `r(varlist)'
              local ++i
              rename `var' x`i'

              save `dta', replace



              Below you can see the results:



              use data1, clear
              list
              +-----------------------------------------------------------------------------------------------------------------------+
              | x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 |
              |-----------------------------------------------------------------------------------------------------------------------|
              1. | -.0658037 1.01091 .4984255 -.1489926 -.8711151 -2.013461 .2881269 .1096137 -1.400732 -.9703687 |
              2. | 2.389028 -1.537572 -.3862164 -.6072646 -2.262745 1.315605 1.686188 -.5404406 1.078409 -.117408 |
              3. | -.2777171 .6730747 -.5674241 -.578813 -.8116008 -.5623083 .7675297 -1.117687 -1.196418 .417776 |
              4. | 2.109452 -1.035937 2.063489 1.183948 -.5243855 -1.020852 -.8674071 -.3530601 -.1752301 1.556753 |
              5. | .7309901 -.6810378 -.9365283 1.818035 .0232499 2.533621 -.5896646 .5805199 .1430279 -1.926774 |
              +-----------------------------------------------------------------------------------------------------------------------+

              use data10, clear
              list

              +-----------------------------------------------+
              | x1 x2 x3 x4 |
              |-----------------------------------------------|
              1. | -.1145661 1.830756 1.860386 .3472159 |
              2. | 1.10806 -.5629539 1.028942 -.7665766 |
              3. | 1.269463 -1.433527 -.6405479 .8663427 |
              4. | .0158674 1.49529 2.840101 -.9815945 |
              5. | .0969952 -.0885036 -2.036327 -.2538646 |
              +-----------------------------------------------+





              share|improve this answer



























                3












                3








                3







                Using some artificially-generated datasets:



                forvalues i = 1 / 10 
                clear
                set obs 5
                forvalues j = 1 / 10
                generate v`j' = rnormal()

                save data`i', replace


                use data10, clear
                drop v5-v10
                save data10, replace
                clear


                Here's an example:



                local allfiles: dir . files "*.dta"

                foreach dta in `allfiles'
                use `dta', clear
                ds v*
                local i 0
                foreach var in `r(varlist)'
                local ++i
                rename `var' x`i'

                save `dta', replace



                Below you can see the results:



                use data1, clear
                list
                +-----------------------------------------------------------------------------------------------------------------------+
                | x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 |
                |-----------------------------------------------------------------------------------------------------------------------|
                1. | -.0658037 1.01091 .4984255 -.1489926 -.8711151 -2.013461 .2881269 .1096137 -1.400732 -.9703687 |
                2. | 2.389028 -1.537572 -.3862164 -.6072646 -2.262745 1.315605 1.686188 -.5404406 1.078409 -.117408 |
                3. | -.2777171 .6730747 -.5674241 -.578813 -.8116008 -.5623083 .7675297 -1.117687 -1.196418 .417776 |
                4. | 2.109452 -1.035937 2.063489 1.183948 -.5243855 -1.020852 -.8674071 -.3530601 -.1752301 1.556753 |
                5. | .7309901 -.6810378 -.9365283 1.818035 .0232499 2.533621 -.5896646 .5805199 .1430279 -1.926774 |
                +-----------------------------------------------------------------------------------------------------------------------+

                use data10, clear
                list

                +-----------------------------------------------+
                | x1 x2 x3 x4 |
                |-----------------------------------------------|
                1. | -.1145661 1.830756 1.860386 .3472159 |
                2. | 1.10806 -.5629539 1.028942 -.7665766 |
                3. | 1.269463 -1.433527 -.6405479 .8663427 |
                4. | .0158674 1.49529 2.840101 -.9815945 |
                5. | .0969952 -.0885036 -2.036327 -.2538646 |
                +-----------------------------------------------+





                share|improve this answer















                Using some artificially-generated datasets:



                forvalues i = 1 / 10 
                clear
                set obs 5
                forvalues j = 1 / 10
                generate v`j' = rnormal()

                save data`i', replace


                use data10, clear
                drop v5-v10
                save data10, replace
                clear


                Here's an example:



                local allfiles: dir . files "*.dta"

                foreach dta in `allfiles'
                use `dta', clear
                ds v*
                local i 0
                foreach var in `r(varlist)'
                local ++i
                rename `var' x`i'

                save `dta', replace



                Below you can see the results:



                use data1, clear
                list
                +-----------------------------------------------------------------------------------------------------------------------+
                | x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 |
                |-----------------------------------------------------------------------------------------------------------------------|
                1. | -.0658037 1.01091 .4984255 -.1489926 -.8711151 -2.013461 .2881269 .1096137 -1.400732 -.9703687 |
                2. | 2.389028 -1.537572 -.3862164 -.6072646 -2.262745 1.315605 1.686188 -.5404406 1.078409 -.117408 |
                3. | -.2777171 .6730747 -.5674241 -.578813 -.8116008 -.5623083 .7675297 -1.117687 -1.196418 .417776 |
                4. | 2.109452 -1.035937 2.063489 1.183948 -.5243855 -1.020852 -.8674071 -.3530601 -.1752301 1.556753 |
                5. | .7309901 -.6810378 -.9365283 1.818035 .0232499 2.533621 -.5896646 .5805199 .1430279 -1.926774 |
                +-----------------------------------------------------------------------------------------------------------------------+

                use data10, clear
                list

                +-----------------------------------------------+
                | x1 x2 x3 x4 |
                |-----------------------------------------------|
                1. | -.1145661 1.830756 1.860386 .3472159 |
                2. | 1.10806 -.5629539 1.028942 -.7665766 |
                3. | 1.269463 -1.433527 -.6405479 .8663427 |
                4. | .0158674 1.49529 2.840101 -.9815945 |
                5. | .0969952 -.0885036 -2.036327 -.2538646 |
                +-----------------------------------------------+






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 22:50

























                answered Nov 13 '18 at 22:44









                Pearly SpencerPearly Spencer

                11k173461




                11k173461























                    1














                    A very easy, not very elegant method would be to check whether the variable exists with capture:



                    foreach dta in `allfiles' 
                    use `dta', clear
                    forval j = 1/1000
                    local varname = strtoname(v`j'[1])
                    cap confirm variable v`j'
                    if (_rc == 0) rename v`j' x`varname'

                    save `dta', replace






                    share|improve this answer

























                    • confirm variable exists for precisely this purpose.

                      – Nick Cox
                      Nov 14 '18 at 9:51











                    • @NickCox not sure why you upvoted this. The loop will not work because there are no 1000 variables in 1 file but across 901 files.

                      – Pearly Spencer
                      Nov 14 '18 at 9:59











                    • You're right, but it can be tweaked easily.

                      – Nick Cox
                      Nov 14 '18 at 10:02











                    • I get _rc == 0 for an existing variable and 111 if the variable does not exist.

                      – E. Sommer
                      Nov 19 '18 at 10:53











                    • you were right, sorry

                      – E. Sommer
                      Nov 19 '18 at 12:09















                    1














                    A very easy, not very elegant method would be to check whether the variable exists with capture:



                    foreach dta in `allfiles' 
                    use `dta', clear
                    forval j = 1/1000
                    local varname = strtoname(v`j'[1])
                    cap confirm variable v`j'
                    if (_rc == 0) rename v`j' x`varname'

                    save `dta', replace






                    share|improve this answer

























                    • confirm variable exists for precisely this purpose.

                      – Nick Cox
                      Nov 14 '18 at 9:51











                    • @NickCox not sure why you upvoted this. The loop will not work because there are no 1000 variables in 1 file but across 901 files.

                      – Pearly Spencer
                      Nov 14 '18 at 9:59











                    • You're right, but it can be tweaked easily.

                      – Nick Cox
                      Nov 14 '18 at 10:02











                    • I get _rc == 0 for an existing variable and 111 if the variable does not exist.

                      – E. Sommer
                      Nov 19 '18 at 10:53











                    • you were right, sorry

                      – E. Sommer
                      Nov 19 '18 at 12:09













                    1












                    1








                    1







                    A very easy, not very elegant method would be to check whether the variable exists with capture:



                    foreach dta in `allfiles' 
                    use `dta', clear
                    forval j = 1/1000
                    local varname = strtoname(v`j'[1])
                    cap confirm variable v`j'
                    if (_rc == 0) rename v`j' x`varname'

                    save `dta', replace






                    share|improve this answer















                    A very easy, not very elegant method would be to check whether the variable exists with capture:



                    foreach dta in `allfiles' 
                    use `dta', clear
                    forval j = 1/1000
                    local varname = strtoname(v`j'[1])
                    cap confirm variable v`j'
                    if (_rc == 0) rename v`j' x`varname'

                    save `dta', replace







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 19 '18 at 12:09

























                    answered Nov 14 '18 at 9:46









                    E. SommerE. Sommer

                    226111




                    226111












                    • confirm variable exists for precisely this purpose.

                      – Nick Cox
                      Nov 14 '18 at 9:51











                    • @NickCox not sure why you upvoted this. The loop will not work because there are no 1000 variables in 1 file but across 901 files.

                      – Pearly Spencer
                      Nov 14 '18 at 9:59











                    • You're right, but it can be tweaked easily.

                      – Nick Cox
                      Nov 14 '18 at 10:02











                    • I get _rc == 0 for an existing variable and 111 if the variable does not exist.

                      – E. Sommer
                      Nov 19 '18 at 10:53











                    • you were right, sorry

                      – E. Sommer
                      Nov 19 '18 at 12:09

















                    • confirm variable exists for precisely this purpose.

                      – Nick Cox
                      Nov 14 '18 at 9:51











                    • @NickCox not sure why you upvoted this. The loop will not work because there are no 1000 variables in 1 file but across 901 files.

                      – Pearly Spencer
                      Nov 14 '18 at 9:59











                    • You're right, but it can be tweaked easily.

                      – Nick Cox
                      Nov 14 '18 at 10:02











                    • I get _rc == 0 for an existing variable and 111 if the variable does not exist.

                      – E. Sommer
                      Nov 19 '18 at 10:53











                    • you were right, sorry

                      – E. Sommer
                      Nov 19 '18 at 12:09
















                    confirm variable exists for precisely this purpose.

                    – Nick Cox
                    Nov 14 '18 at 9:51





                    confirm variable exists for precisely this purpose.

                    – Nick Cox
                    Nov 14 '18 at 9:51













                    @NickCox not sure why you upvoted this. The loop will not work because there are no 1000 variables in 1 file but across 901 files.

                    – Pearly Spencer
                    Nov 14 '18 at 9:59





                    @NickCox not sure why you upvoted this. The loop will not work because there are no 1000 variables in 1 file but across 901 files.

                    – Pearly Spencer
                    Nov 14 '18 at 9:59













                    You're right, but it can be tweaked easily.

                    – Nick Cox
                    Nov 14 '18 at 10:02





                    You're right, but it can be tweaked easily.

                    – Nick Cox
                    Nov 14 '18 at 10:02













                    I get _rc == 0 for an existing variable and 111 if the variable does not exist.

                    – E. Sommer
                    Nov 19 '18 at 10:53





                    I get _rc == 0 for an existing variable and 111 if the variable does not exist.

                    – E. Sommer
                    Nov 19 '18 at 10:53













                    you were right, sorry

                    – E. Sommer
                    Nov 19 '18 at 12:09





                    you were right, sorry

                    – E. Sommer
                    Nov 19 '18 at 12:09

















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290438%2fhow-to-use-a-forval-loop-and-take-into-account-one-exception%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

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Darth Vader #20

                    Syphilis