Announcement

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

  • Converting multiple .csv files into .dta using a loop

    I have a directory which contains 731 .csv files. I would like to convert all of them to .dta before appending all the data. This is the code I have so far:

    local csv_directory "C:\Users\Spenc\Desktop\India\India2"
    mkdir "`csv_directory'/stata"
    clear

    local myfilelist : dir "`csv_directory'" files "*.csv"

    foreach file of local myfilelist {
    import delimited "`file'", clear delim(",") bindquote(strict) maxquotedrows(0)
    local outfile = subinstr("`file'",".csv","",.)
    save "`C:\Users\Spenc\Desktop\India\India2`outfile' ", replace
    }

    My very first CSV file gets the error "file `C:\Users\Spenc\Desktop\India\India2indiamart_24-parganas.dta could not be opened." It shouldn't reading it as a dta, but csv.

    Does anyone see what I be doing wrong here? Much Appreciation.

    -Tyler

  • #2
    That message is coming from the save command and is telling you that it was unable to open the given filename for output. The problem in the specific example you show is that you have an unnecessary ` before the C: - note that the error message includes that.

    Perhaps you wanted
    Code:
    save "`csv_directory'/stata/`outfile'", replace

    Comment


    • #3
      Oh, thanks! It ran once I removed that, but did not save any dta files.

      Correct, there is no filename.dta in the new stata folder created. When I tried your example code I still received that error, although now it's trying to pull it from the created stata directory. "file /stata/indiamart_24-parganas.dta could not be opened," but a dta file has not been created yet.

      Comment


      • #4
        Again, Stata is telling you it cannot create that file. Why? Look at the path: it's missing the entire substitution for `csv_directory' and why is that?

        I believe your problem is that you have written your code in the do-file editor window, and then rather than running everything at once, you are running it by selecting a few lines and running them, then selecting the next few lines and running them, and so on.

        Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
        Code:
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . local message Hello, world.
        
        . display "The message is `message'"
        The message is Hello, world.
        
        .
        end of do-file
        Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
        Code:
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . local message Hello, world.
        
        .
        end of do-file
        
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . display "The message is `message'"
        The message is
        
        .
        end of do-file
        The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

        Comment

        Working...
        X