Announcement

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

  • A loop to run multiple do files

    Dear Statalist users,

    First-time poster here. I am trying to run many do files that will transform my .dat files into .dta files. They are all in the same folder, but are not necessarily named uniformly. Usually, they take the general pattern of Z01 through Z17, but often have letters following, so Z02A Z02B, and it is not a consistent pattern.

    I have found the -: dir- command, but it does not seem to be running the files. I do not have much code, all I have written so far is

    local files: dir "directory" files "*.do"
    foreach file in `files' {
    do`files', clear
    }

    I am running Stata 14 at the moment. Thank you in advance!!

  • #2
    Welcome to Statalist.

    Perhaps this will solve your problem.
    Code:
    local files: dir "directory" files "*.do"
    foreach file in `files' {
        do`file', clear
    }
    If it does not, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    ...
    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
    It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using CODE delimiters, as described in section 12 of the FAQ.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.
    Last edited by William Lisowski; 06 May 2018, 12:43.

    Comment


    • #3
      Too late to edit what I wrote above, I see another problem which I overlooked, in part because it was not obvious in post #1 because of the lack of CODE formattig. Here is a further correction. In summary, compared to the version in post #1, this version replaces `files' with `file' and adds a space between it and the do that precedes it.
      Code:
      local files: dir "directory" files "*.do"
      foreach file in `files' {
          do `file', clear
      }

      Comment


      • #4
        Dear Statalist users,

        I follow the code below to run multiple do files and I find that this loop cannot stop. How to run the do file one by one and only run once ?
        Code:

        local files: dir "directory" files "*.do" foreach file in `files' { do `file', clear }

        Comment


        • #5
          Hello,

          I have in the same folder 180 do-files that are named as follows:
          kmeans-AFG.do
          kmenas-AGO.do

          And so on

          I am trying to use the command suggested by William Lisowski:

          Code:
          local files: dir "C:\Users\Inspiron\OneDrive\academic\articles\2020\grouped-fixed-effects\GFE-ours-estimation\firstdiff" files "*.do"
          
          foreach file in `files' { 
             do `file' }
          However, it is reporting the following error:

          Code:
          file kmeans-AFG.do not found
          r (601);
          
          end of do-file
          
          r (601);
          I'm using STATA 14.2 and OS Windows 10.

          Thanks in advance for your attention!

          Comment


          • #6
            \delete
            Last edited by Sonia Chen; 26 Aug 2021, 19:29.

            Comment


            • #7
              Originally posted by Alexandre Loures View Post
              Hello,

              I have in the same folder 180 do-files that are named as follows:
              kmeans-AFG.do
              kmenas-AGO.do

              And so on

              I am trying to use the command suggested by William Lisowski:

              Code:
              local files: dir "C:\Users\Inspiron\OneDrive\academic\articles\2020\grouped-fixed-effects\GFE-ours-estimation\firstdiff" files "*.do"
              
              foreach file in `files' {
              do `file' }
              However, it is reporting the following error:

              Code:
              file kmeans-AFG.do not found
              r (601);
              
              end of do-file
              
              r (601);
              I'm using STATA 14.2 and OS Windows 10.

              Thanks in advance for your attention!
              Hi, Alexandre,

              Not sure if you've solved the issue, but I figured your question out, written below is the code that works:

              Code:
              local files: dir "C:\Users\Inspiron\OneDrive\academic\articles\2020\grouped-fixed-effects\GFE-ours-estimation\firstdiff" files "*.do"
              
              cd "C:\Users\Inspiron\OneDrive\academic\articles\2020\grouped-fixed-effects\GFE-ours-estimation\firstdiff"
              
              foreach file in `files' {
              do `file' }
              Please give it a try.

              Comment

              Working...
              X