Announcement

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

  • Converting all files in a folder from .sav to .dta format

    Hello,

    I am trying to convert all the files in a folder from spss to stata format. I went through some of the relevant posts and tried the suggestions but I keep getting some error. For example, if I try the code below, in which the file names are given individually as a, b, c, d, e and f:

    foreach i in a b c d e f{
    import spss using "\Desktop\Datasets\country\`i'.sav", clear
    save "\\Desktop\Datasets\country\`i'.dta", replace
    }


    I get the error as:

    file \\Desktop\Datasets\country\`i'.sav not found


    Is there something wrong with the way I am calling the filenames in the loop. I would like to make the above code work please, as I can use the same structure for some other work as well.


    Thanks for your help.


    Regards
    Vignesh


  • #2
    The backslash \ is an escape character which makes Stata interpret the next letter literally. So your macro substitution is not working. Even when you are on Windows systems, it is best to use forward slashes / in Stata when specifying directory paths, for precisely this reason.

    Just change all your backslashes to forward slashes, and it should work.

    Comment


    • #3
      try double backslashes (or a single forward slash) next to the local macro,
      Code:
      "... \country\\`i'.sav"
      Last edited by Øyvind Snilsberg; 31 Oct 2022, 01:31. Reason: edit: crossed with #2

      Comment


      • #4
        Thanks for your kind help, Himanshu and Snilsberg. I tried both of your solutions and it worked.

        Regards
        Vignesh.

        Comment

        Working...
        X