Announcement

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

  • read multiple dta files in a directory and save in older stata version in another directory

    Hello

    I am a new stata user and would like to read multiple dta files in a directory and save them in stata 12 version with the same name but a suffix _stata12 in another directory. The dta files have different variables and sizes, I dont want to append them together, just need to use the saveold command to save them in another folder. Since I have around hundreds of them, I guess there must be a way to write a loop for this purpose.

    Can someone help me with this?

    Many thanks in advance.

    Bente

  • #2
    The basic setup is something like this

    Code:
    cd "origin_folder"
    local dta : dir . files "*.dta"
    foreach fn of local dta {
        use "`fn'" , clear
        gettoken name : fn , parse(".")
        saveold "destination_folder/`name'_stata12.dta" , version(12)
    }
    Best
    Daniel

    Comment


    • #3
      Code:
      // get a list of files with the extension .dta in directory old/dir
      local files : dir "old/dir" files "*.dta"
      
      // remove quotes
      local files : list clean files
      
      // loop over files
      foreach file of local files {
          // open the file
          use old/dir/`file'
          
          // whatever is left of a "." in local file is in local name,
          // the rest in local ext
          gettoken name ext : file, parse(".")
          
          // save the file in old version with new name
          saveold new/dir/`name'_stata12`ext', version(12)
      }
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Originally posted by daniel klein View Post
        The basic setup is something like this

        Code:
        cd "origin_folder"
        local dta : dir . files "*.dta"
        foreach fn of local dta {
        use "`fn'" , clear
        gettoken name : fn , parse(".")
        saveold "destination_folder/`name'_stata12.dta" , version(12)
        }
        Best
        Daniel
        Hello Daniel

        Thanks for the very good tip, I think I understand how the loop now works. I put in my directory and run the code, but nothing happened in stata. Do you have any experience why this is the case?

        Many thanks again.

        Best
        Bente

        Comment


        • #5
          Originally posted by Maarten Buis View Post
          Code:
          // get a list of files with the extension .dta in directory old/dir
          local files : dir "old/dir" files "*.dta"
          
          // remove quotes
          local files : list clean files
          
          // loop over files
          foreach file of local files {
          // open the file
          use old/dir/`file'
          
          // whatever is left of a "." in local file is in local name,
          // the rest in local ext
          gettoken name ext : file, parse(".")
          
          // save the file in old version with new name
          saveold new/dir/`name'_stata12`ext', version(12)
          }
          Hello Maarten

          Thanks for the code and the detailed explanation. I think I understand how it supposes to work. When I put in my directory and run the code somehow, nothing happened in stata. I dont have any experience in stata. If you know SAS, it looks like I've written a macro and didnt excute it. Do you know why this is the case?

          Many thanks again.

          Best
          Bente

          Comment


          • #6
            Please show use what you have typed in Stata (exactly) and what Stata did in response (exactly). Use code delimters as Maarten and i have done. I have no clear idea what you mean when you say "nothing happend".

            On second thought, show us the result of

            Code:
            dir "old/dir/*.dta"
            Best
            Daniel
            Last edited by daniel klein; 11 Sep 2017, 11:35.

            Comment


            • #7
              Originally posted by daniel klein View Post
              Please show use what you have typed in Stata (exactly) and what Stata did in response (exactly). Use code delimters as Maarten and i have done. I have no clear idea what you mean when you say "nothing happend".

              On second thought, show us the result of

              Code:
              dir "old/dir/*.dta"
              Best
              Daniel
              Hello Daniel

              Wenn I type in dir "R:\XX\YY\A\*.dta" I got the list of files that I have in my old dir.

              What I gave in stata is exactly your code, only I am using stata Windows so I Change "/" to "" , so it was something like
              cd "R:\XX\YY\A"
              local dta : dir . files "*.dta"
              foreach fn of local dta {
              use "`fn'" , clear
              gettoken name : fn , parse(".")
              saveold "R:\XX\YY\B\`name'_stata12.dta" , version(12)
              }

              In stata log stata simply showed what I typed in with "end of do-file" but nothing is read into stata and nothing is saved.

              Maybe the way I changed Directory was wrong?

              Thanks so much for your help!

              BR Bente

              Comment


              • #8
                Originally posted by Bente Mueller View Post
                only I am using stata Windows so I Change "/" to "[\]"
                Do not do this. Windows does not care whether you use a slash or backslash, Stata does. For more information see Cox (). Also, Stata is casesensitive, Windows is not. Try

                Code:
                local dta : dir . files "*.dta" , respectcase
                Make sure to run (or do) the complete code, not only execute parts of it.

                Best
                Daniel

                Cox, Nicholas J. 2008. Stata tip 65: Beware the backstabbing backslash. The Stata Journal, 8(3), pp. 446–447 .

                Comment


                • #9
                  Originally posted by daniel klein View Post

                  Do not do this. Windows does not care whether you use a slash or backslash, Stata does. For more information see Cox (). Also, Stata is casesensitive, Windows is not. Try

                  Code:
                  local dta : dir . files "*.dta" , respectcase
                  Make sure to run (or do) the complete code, not only execute parts of it.

                  Best
                  Daniel

                  Cox, Nicholas J. 2008. Stata tip 65: Beware the backstabbing backslash. The Stata Journal, 8(3), pp. 446–447 .
                  Run the complete code has solved the Problem! I ran cd and local separated from the loop! That was the problem!

                  Thank your Daniel!

                  BR Bente

                  Comment

                  Working...
                  X