Announcement

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

  • how to use local with fs properly when setting the directory

    Hello,

    I am struggling with using local in my code.
    Here is the sample code.

    HTML Code:
    cd "D:\Dropbox\research\source_data"
    local dopath "D:\Dropbox\research\stata_files"
    
    clear
    fs "`dopath'\2005*.do"
    return list
    local dofile = r(files)
    do "`dopath'\`dofile'"
    My objective is to execute the dofile named "2005_labor_20230921_21352" located in the "D:\Dropbox\research\stata_files" directory.
    Since the filename is quite long and I need to perform similar tasks with other files, I employed the fs command to simplify the implementation.
    However, I encountered an issue with the error message stating "file D:\Dropbox\research\stata_files`dofile'.do not found."

    Thank you.

  • #2
    Your problem arises with -do "`dopath'\`dofile'"-. Although you intend \ to be the Windows path separator and ` to introduce local macro dofile, when Stata sees \` together, it takes that digraph to be a literal backtick (`) character instead. There are various ways to resolve this difficulty, but I think the simplest is to use / rather than \ as your path separator. (This works in Windows just as well as it does in Mac and Unix.) And it has the advantage of making your code compatible across platforms.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Your problem arises with -do "`dopath'\`dofile'"-. Although you intend \ to be the Windows path separator and ` to introduce local macro dofile, when Stata sees \` together, it takes that digraph to be a literal backtick (`) character instead. There are various ways to resolve this difficulty, but I think the simplest is to use / rather than \ as your path separator. (This works in Windows just as well as it does in Mac and Unix.) And it has the advantage of making your code compatible across platforms.

      Thank you very much for your suggestion. I just fixed the code and it now works!
      Last edited by Chang Seok; 21 Sep 2023, 22:54.

      Comment

      Working...
      X