Announcement

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

  • forvalues to import different csv files

    Dear Stata users:

    I hope you are all doing well.

    I receive the following error when trying to run the following code:

    -> error message: file D:\overview\Missouri.csv not found

    -> code:
    .
    clear*
    forvalues i = 1(1)150 { // Adjust the range as needed

    // Import CSV file based on the current value of `i`
    import delimited "D:\overview\Missouri\`i'.csv", stringcols(23)


    // Save as Stata file with adjusted filename
    save "D:\overview\Missouri\`i'a.dta", replace
    }


    Could you please highlight what is wrong about the code since the file location is correct? Thanks in advance!

  • #2
    Code:
    import delimited "D:\overview\Missouri\`i'.csv", stringcols(23)
    That's what's wrong with it.

    In Stata, the digraph \` is not interpreted as \ (path separator) followed by ` (introduction of a local macro reference). Rather it is interpreted as a single literal ` character. This is known as an escape sequence, and there are several such situations where \ followed by some character has a special meaning that is not \ + that character. That is, Stata is now looking of a file whose name is D:\overview\Missouri`i'.csv. So it is looking in the wrong directly (D:\overview, not D:\overview\Missouri) for a file whose name is Missouri`i'.csv. And, unsurprisingly, it doesn't find any such file.

    There are two ways to resolve this problem. One is to replace \` with \\`. Because \\ is read by Stata as an escape sequence on \, so that \\ is interpreted as a single literal backslash character rather than as the start of an escape sequence. The other is to use / as your path separator instead of \. Even though Windows' preferred path separator is \, this is not a problem because Windows accepts / in this role. My preference is to use / because it has the advantage of making your code compatible with Mac and Unix (where / is the path separator and \ is not accepted as a substitute.)

    Comment


    • #3
      My understanding is that Windows never sees the / to understand it as intended; Stata does the translation on your behalf.

      See also [U] 18.3.11 in your PDF documentation or https://journals.sagepub.com/doi/pdf...867X0800800310



      Comment


      • #4
        Re #3: That may well be the case; I have seen that before. But it is also the case that if you open a command window in Windows, so you are communicating directly with Windows without intermediation by Stata, you can use / as the path separator in your commands and everything works normally.

        Comment


        • #5
          Some Googling underlines and slightly qualifies Clyde's point. Windows often does allow / but seemingly not always. I was surprised!

          The discussion at https://retrocomputing.stackexchange...path-separator seems to catch the flavour.

          Comment


          • #6
            Dear Nick and Clyde: Many thanks for helping me out! You are both amazing persons!

            Comment

            Working...
            X