Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Manipulating with many files

    Hello!
    I have 12 files from years from 2006 to 2017 and I have one code that I want to run in all of them. I thought the following works:
    foreach n in seq(), from=2006 to=2017 {
    display "'n'"
    use "C:\Users\Admin\Documents\'n'.dta"
    *main code
    }
    But it doesn't work. I am a beginner in STATA, please, could you say how to do it in a proper way?
    Thank you in advance for your help!

  • #2
    Maybe try something closer to the following.
    Code:
    forvalues n = 2006/2017 {
        display in smcl as result `n'
        use C:\Users\Admin\Documents/`n', clear
        // Remaining code
    }

    Comment


    • #3
      Expanding on post #2, in In Stata 16 (at least, perhaps in earlier versions as well) see section 18.3.11 of the Stata User's Guide PDF (included in your Stata installation and accessible through Stata's Help menu) for an explanation of the use of forward- and backward-slashes in file paths.

      The backslash character
      Code:
      \
      has meaning to Stata: it will prevent the interpretation of any following character that has a special meaning to Stata, in particular
      Code:
      `
      will not be interpreted as indicating a reference to a macro.

      But all is not lost: Stata will allow you to use the forward slash character in path names on any operating system, and Windows will not object. My understanding is that every version of Windows has accepted "/" as a path separator, and every version of MS-DOS beginning with DOS 2.0 (the first version that had subdirectories). Only in command lines was "/" not allowed, because it had already been used as a switch delimiter in MS-DOS 1.0.
      Code:
      . local prefix batch42
      
      . display "Q:\HOME\`prefix'_filename"
      Q:\HOME`prefix'_filename
      
      . display "Q:/HOME/`prefix'_filename"
      Q:/HOME/batch42_filename

      Comment


      • #4
        Originally posted by Joseph Coveney View Post
        Maybe try something closer to the following.
        Code:
        forvalues n = 2006/2017 {
        display in smcl as result `n'
        use C:\Users\Admin\Documents/`n', clear
        // Remaining code
        }
        Joseph, I tried, but I got a mistake: "'n' invalid name". Perhaps, you can recommend what to do.

        Comment


        • #5
          Originally posted by William Lisowski View Post
          Expanding on post #2, in In Stata 16 (at least, perhaps in earlier versions as well) see section 18.3.11 of the Stata User's Guide PDF (included in your Stata installation and accessible through Stata's Help menu) for an explanation of the use of forward- and backward-slashes in file paths.

          The backslash character
          Code:
          \
          has meaning to Stata: it will prevent the interpretation of any following character that has a special meaning to Stata, in particular
          Code:
          `
          will not be interpreted as indicating a reference to a macro.

          But all is not lost: Stata will allow you to use the forward slash character in path names on any operating system, and Windows will not object. My understanding is that every version of Windows has accepted "/" as a path separator, and every version of MS-DOS beginning with DOS 2.0 (the first version that had subdirectories). Only in command lines was "/" not allowed, because it had already been used as a switch delimiter in MS-DOS 1.0.
          Code:
          . local prefix batch42
          
          . display "Q:\HOME\`prefix'_filename"
          Q:\HOME`prefix'_filename
          
          . display "Q:/HOME/`prefix'_filename"
          Q:/HOME/batch42_filename
          William, thank you for your answer. I have read 18.3.11 in Guide, but, unfortunately, I don't understand how to make my code correct (to apply 'the main code' for all 12 files, which are name as "2006.dta, 2007.dta, 2008.dta, ..., 2016.dta, 2017.dta").

          Comment


          • #6
            Joseph, I tried, but I got a mistake: "'n' invalid name".
            That is because you did not copy Joseph's code correctly. The foreach loop creates a local macro named n. To have Stata substitute the value of a local macro
            Code:
             n
            you need to type on your keyboard
            Code:
             `n'
            (as Joseph did) where the first character is (on an American English keyboard) the "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key. That is not what you typed, The error message tells us you typed a "single quote" ("apostrophe") before the n, as below
            Code:
             'n'

            Comment


            • #7
              Originally posted by William Lisowski View Post

              That is because you did not copy Joseph's code correctly. The foreach loop creates a local macro named n. To have Stata substitute the value of a local macro
              Code:
               n
              you need to type on your keyboard
              Code:
               `n'
              (as Joseph did) where the first character is (on an American English keyboard) the "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key. That is not what you typed, The error message tells us you typed a "single quote" ("apostrophe") before the n, as below
              Code:
               'n'
              William, thank you very much.
              I will hone my skills as a user of STATA.

              Comment


              • #8
                Originally posted by Joseph Coveney View Post
                Maybe try something closer to the following.
                Code:
                forvalues n = 2006/2017 {
                display in smcl as result `n'
                use C:\Users\Admin\Documents/`n', clear
                // Remaining code
                }
                Joseph, thanks a lot.
                Sorry that I don't understand signs at once. William showed me where I wrote incorrect.

                Comment

                Working...
                X