Announcement

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

  • Looping through specific files in multiple subdirectories?

    I have a directory of subdirectories corresponding to months/ years with each subdirectory named (subyymm, ex: sub1712). I have subdirectories for each month from sub1712 - sub1812. Within each subdirectory is a .txt file called "sub_data.txt', and this is consistent across all subdirectories. I was wondering would would be a quick way to loop through each subdirectory and append all 13 sub_data.txt files together? Right now I append one at a time, and this seems inefficient.

    Thank you!

  • #2
    Welcome to Statalist.

    I'm not sure how you are measuring inefficient. Do you mean "takes the computer to long to run the code" or do you mean "takes the programmer too long to copy and modify the code 13 times"?

    If the latter, perhaps something like the following untested code will point you in a useful direction.
    Code:
    clear
    save all13, emptyok
    foreach ym in 1712 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 {
        import delimited ".../directory/sub`ym'/sub_data.txt", ....
        // create a  variable that lets you identify the source of each observation
        generate str4 source = "`ym'"
        append using all13
        save all13, replace
    }

    Comment

    Working...
    X