Announcement

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

  • Loop over *dta files to export to csv files

    Dear Stata users

    I have 132 *dta files (X1.dta to X132.dta) in the folder which must be exported to *txt file format and saved as 132 *txt files (x1.txt to x132.txt). I run the following code:

    Code:
    cd "C:\Users\by month"
    local allfiles : dir . files "*.dta"
    foreach f in `allfiles' {
    use `f', clear
    export delimited using `""C:\Users\by month\`m'"', replace
    }
    Can you see where is the error in the code?

  • #2
    Code:
    clear
    cd "C:\Users\by month"
    local allfiles : dir . files "*.dta"
    foreach f in `allfiles' {
        use `f', clear
        local m = subinstr("`f'","dta","txt",.)
        export delimited using `m', replace
    }
    Or

    Code:
    clear
    cd "C:\Users\by month"
    forval f = 1/132 {
        use X`f'.dta, clear
        export delimited using x`f'.txt, replace
    }
    Last edited by Ali Atia; 13 Jun 2021, 20:37.

    Comment


    • #3
      Ali, thanks for a prompt response. Both codes return the following error:

      file x1.dta not Stata format
      r(610);
      Any idea why?

      Comment


      • #4
        Ali, in fact your code works. The issue was with *dta files which for some reason were not recognized by stata. Thank you.

        Comment

        Working...
        X