Announcement

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

  • Renaming data files in a folder

    Hi all, I am a Stata 16 user. I work in this project where we have to download data in .xlsx forms. The forms do not retain the same name with every download as the date of import is appended on the end of the file name. This generally makes it harder to download and run a cleaning and merging do file. To make the process a little bit automatic, in have used
    xls2dta : import excel . , firstrow allstring
    to help me turn the respective files to .dta files. Now I believe if I could shell rename the files (there are 31 of them) into numbers from 1-31 then this would allow me to easily merge them without necessarily handling the string names. I wrote a loop to do this but from my end it do produces no error or no output. The file names do not change. my code is

    **** list of all files in the folder
    local fils : dir . files "*.dta"
    di `fils'
    **** sort the file list
    local fils: sort `fils'

    **** renaming files sequentially. no 1-31

    local i = 1
    foreach file of local fils {
    local newname "`i'.dta"
    shell rename "`file'" "`newname'"
    di "rename `file' to `newname'"
    local i = `i' + 1
    }
    Any help to debug this will be highly appreciated.

    Regards

  • #2
    I am surprised that you are not seeing any error message, because when I run it (version 18, but I don't think it makes a difference in this regard):

    Code:
    . local fils: sort `fils'
    invalid syntax
    r(198);
    When I fix that line to read:
    Code:
    local fils: list sort fils
    it runs with no error messages and produces the desired results. Note the inclusion of the keyword list, and the removal of macro-quotes from fils.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      I am surprised that you are not seeing any error message, because when I run it (version 18, but I don't think it makes a difference in this regard):
      If you run the chunks separately you should not receive any error. But I can always discard that part of the code. My biggest problem is in the loop

      Comment


      • #4
        If you run the chunks separately, you still receive the error. But more important, if you run the chunks separately the loop will not run at all--that is your problem. Don't run them separately.

        Remember how local macros work. When you run a chunk, at the end of the chunk, any local macros that were defined in it disappear. So if you are running this in chunks, before you start the loop, the local macro fils disappears. So then when Stata gets to -foreach file of local fils-, local fils is an empty string. So Stata says: nothing to do here. And the loop is never executed.

        Comment


        • #5
          **** renaming files sequentially. no 1-31
          local i = 1
          foreach file of local fils {
          local newname "`i'.dta"
          shell rename "`file'" "`newname'"
          di "rename `file' to `newname'"
          local i = `i' + 1
          }
          I run this loop as a chunk of course. But after running I receive no error and the file names don't change.

          Comment


          • #6
            Oh. I have now realised what you mean. Yes I need to run the whole chunk as one. Can you allow me to retry before I give you the feedback. My windows machine is updating and I should be able to retry in a short while.

            Comment


            • #7
              Thank you for pointing that out. Leaving out that section of code works and I believe if anyone needs my help then they can go to my initial code.

              Comment

              Working...
              X