Announcement

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

  • How to change a directory inside a loop?

    My problem is that I need to change a directory before I save the files of an specific directory. I want to save the files in an output folder in another directory but if I change the directory inside the loop, it remains in the same directory

    cd "$IN"
    cd "C:\Users\...."
    local myfile: dir "C:\Users\..." files "*.txt"
    foreach file of local myfile {
    di "`file'"
    insheet using "`file'",clear
    local outfile = subinstr("`file'",".txt"," ",.)
    save "`file'.dta",replace
    }
    **
    Last edited by Francisco Mejia; 06 May 2016, 09:12.

  • #2
    The command to change the directory is not inside the loop. To change it inside the loop, do so after the foreach line. You can also change the save command to include the directory:

    Code:
    save "c:/users/..../`file.dta"
    notice the direction of the slashes. It matters because "\`file'" will prevent the macro expansion.
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      thank you very much Carole.

      Comment

      Working...
      X