Announcement

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

  • Append 50 datasets using foreach (loop)

    Hi everyone:

    I need to append 50 datasets. The datasets were saved as data1.dta data2.dta... I tried by this way:

    use "E:\Backup\test\data1.dta", clear
    foreach num of numlist 2/50 {
    append using data`num'
    }

    But it was not work. The message of error was: file data2 not found
    Please, is it possible to see the problem with the commands?
    Thanks a lot.
    Ana Elisa

  • #2
    The code looks correct to me. But the error message bothers me. In general, with file handling commands, when no filename extension is given, Stata will impute the extension appropriate to the command (e.g. dta for -use-, -save-; .xlsx for -import excel-, etc.) And the documentation for -append- explicitly says that it adds .dta to the filename if there is no extension. And if I try to -use- a non-existent file that I reference with no extension, the error message does add the .dta extension:
    Code:
    . use non_existent, clear
    file non_existent.dta not found
    r(601);
    But your error message doesn't have a .dta in it. It just says it can't find data2. Which leads me to think that -append- might not be adding the .dta filename extension as it is supposed to.

    So I would try:
    Code:
    foreach num of numlist 2/50 {
        append using data`num'.dta
    }
    If that works, it is evidence that there is a bug in -append-, and you should notify tech support so they can fix -append- in a future update.

    That said, I cannot replicate the problem you are having in my setup. (Stata 18 MP4, Windows 64-bit) When I arrange a series of consecutively numbered files like this and use your exact code, it runs correctly.

    If that doesn't work, then I'm inclined to believe that file data2.dta is somehow missing from your working directory, and you should try to find it and put it there.
    Last edited by Clyde Schechter; 10 Dec 2023, 21:00.

    Comment


    • #3
      I strongly suspect that the path to each file needs to be specified, or else the directory needs to first be changed to where your datasets are located. I would not (yet) suspect a bug with append.

      you can add the following line before your loop, where you replace the path with the correct one for your computer.

      Code:
       cd “path/to/datasets”

      Comment


      • #4
        Dear Clyde and Leonardo:

        Many thanks to answer to me so fast! Clyde, I tried your suggestion and the problem persist (file data2 not found).
        Leonardo, please, is it possible to help me how can configurate the command. All datasets are saved in this diretory (E:\Backup\test"). How should I changed? as I suggest.
        It is very curious that this same command worked two week ago. I constructed the command since Statalist suggestion (years ago).
        Thanks a lot.
        Ana Elisa

        Comment


        • #5
          Code:
          cd "E:\Backup\test"
          
          use data1.dta, clear
          
          forval num = 2/50 {
               append using data`num'
          }

          Comment


          • #6
            Dear Nick Cox:

            Thank you very much! The suggested command worked!
            best regards,
            Ana

            Comment

            Working...
            X