Announcement

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

  • Dynamically open files stata

    I am trying to write a loop to dynamically open files in stata. I want to open two files, the first one is named forex_US.csv and the second one is named firex_US.csv .
    clear all set varabbrev off set scheme s1mono

    Code:
    cd "D:\Test\"  
    global data "data_folder"  
     ************** * Load data * **************  
    local mylist forex firex
    foreach i of local mylist {
     import delimited "${data}\`i'_US.csv", stringcols(1 2) clear

    However, the code above does not seem to recognize the `i' .

    What am I doing wrong?
    Last edited by Nathan Clark; 24 Dec 2022, 12:29.

  • #2
    The problem is that you are using \ as your path separator. \ is also used as an "escape character" in Stata, so that \` is interpreted as a literal backtick rather than as denoting the start of a local macro reference. There are two ways you can deal with this. One is to use \\ instead of \: \\ escapes the escape functioning of \, so that \\` is interpreted as a literal backslash followed by the opening of a local macro reference. The other, which I think is simpler and clearer, is to use / as your path separator. While \ is the normal path separator in Windows, Windows also recognizes / as a path separator. And Stata always recognizes / as a path separator and feeds Windows the appropriate information in I/O commands.

    Comment


    • #3
      It's fixed. Thank you.

      Comment

      Working...
      X