Announcement

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

  • import multiple .csv into Stata

    I have 20 .csv files which are 2001.csv 2002.csv...... 2020.csv, and I intend to import them into Stata. The following is the command:

    forvalues i=2001/2020 {
    clear
    import delimited 'i'.csv
    save 'i'.dta,replace
    }

    But the command window shows that "i" is invalid. I don't know what the problem is, andI need your advice urgently. Thank you!

  • #2
    It isn't 'i' in Stata syntax. It's `i'. On a US keyboard, that character is found as the lower case character located just to the right of the 1! key.

    Comment


    • #3
      It looks like `i' starts with a single quote in the code you've provided, but it should start with a back-tick symbol (`). It's the one that shares a key with the tilde (~) symbol. I see you are importing a set of csv files and outputting the same file as a .dta. This will not load all of the data into Stata at once. Is this the intended behavior?

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        It isn't 'i' in Stata syntax. It's `i'. On a US keyboard, that character is found as the lower case character located just to the right of the 1! key.
        Now I have changed the command to the following one:
        forvalues i=2001/2020 {
        clear
        import delimited BACI_HS96_1996-2020-CEPII\\ `i'.csv
        save tradeflows\\`i'.dta,replace
        }

        The Stata command shows
        using required
        What should I do now?
        Last edited by Kendal zhang; 09 Aug 2022, 20:17.

        Comment


        • #5
          Originally posted by Daniel Schaefer View Post
          It looks like `i' starts with a single quote in the code you've provided, but it should start with a back-tick symbol (`). It's the one that shares a key with the tilde (~) symbol. I see you are importing a set of csv files and outputting the same file as a .dta. This will not load all of the data into Stata at once. Is this the intended behavior?
          Thank you! How can I load all of the data into Stata at once?

          Comment


          • #6
            file 2001.csv not found
            It looks like Stata was unable to find a file named "2001.csv" in the working directory on your hard drive. You can either change the working directory with the -cd- command, or specify the full path to the file in the string that represents the file name.

            Thank you! How can I load all of the data into Stata at once?
            This is easier said than done. You are asking to do something advanced, but you don't appear to know some of the basics. that being said, you may want to look at the documentation for the -merge- command. Basically, you probably want to merge all of these different .csv files into a single table. Type this into the Stata console:

            Code:
            help merge

            Comment


            • #7
              Originally posted by Daniel Schaefer View Post

              It looks like Stata was unable to find a file named "2001.csv" in the working directory on your hard drive. You can either change the working directory with the -cd- command, or specify the full path to the file in the string that represents the file name.



              This is easier said than done. You are asking to do something advanced, but you don't appear to know some of the basics. that being said, you may want to look at the documentation for the -merge- command. Basically, you probably want to merge all of these different .csv files into a single table. Type this into the Stata console:

              Code:
              help merge
              Thank you!

              Comment


              • #8
                Originally posted by Daniel Schaefer View Post

                It looks like Stata was unable to find a file named "2001.csv" in the working directory on your hard drive. You can either change the working directory with the -cd- command, or specify the full path to the file in the string that represents the file name.



                This is easier said than done. You are asking to do something advanced, but you don't appear to know some of the basics. that being said, you may want to look at the documentation for the -merge- command. Basically, you probably want to merge all of these different .csv files into a single table. Type this into the Stata console:

                Code:
                help merge
                Now I have changed the command to the following one:
                forvalues i=2001/2020 {
                clear
                import delimited BACI_HS96_1996-2020-CEPII\\ `i'.csv
                save tradeflows\\`i'.dta,replace
                }

                The Stata command shows
                using required
                What should I do now?

                Comment


                • #9
                  Code:
                  import delimited using "BACI_HS96_1996-2020-CEPII\\`i'.csv", clear
                  but save will only write the file to the filesystem as a .dta file.

                  Comment


                  • #10
                    If you're using \\ as a separator for the folder or directory, you should instead use the forward slash /. Your code might become:
                    Code:
                    forvalues i=2001/2020 {
                        import delimited using "BACI_HS96_1996-2020-CEPII/`i'.csv", clear
                        save "tradeflows/`i'.dta", replace
                    }

                    Comment


                    • #11
                      Originally posted by Daniel Schaefer View Post
                      Code:
                      import delimited using "BACI_HS96_1996-2020-CEPII\\`i'.csv", clear
                      but save will only write the file to the filesystem as a .dta file.
                      I know, thank you!I just want to save the .csv files as Stata form firstly, and after that I will append the different .dta.

                      Comment


                      • #12
                        Originally posted by Hemanshu Kumar View Post
                        If you're using \\ as a separator for the folder or directory, you should instead use the forward slash /. Your code might become:
                        Code:
                        forvalues i=2001/2020 {
                        import delimited using "BACI_HS96_1996-2020-CEPII/`i'.csv", clear
                        save "tradeflows/`i'.dta", replace
                        }
                        You are great, the code works well. The whole moring I have been troubled by this problem, thank you so much. I watched a video, and it showed that if there were apostrophe in the code we should use \\, is it right? Are you mean that the different slashes in the code should be unified?

                        Comment


                        • #13
                          Actually, I think \\ would have worked. But I prefer / because then I don't have to worry about the fact that Stata treats \ as an escape character. And / has the added advantage that it works across different OSes -- macOS, Linux, and Windows as well.

                          The problem in your code was probably because you had a space between \\ and `i',

                          import delimited BACI_HS96_1996-2020-CEPII\\◼︎`i'.csv

                          Comment


                          • #14
                            Originally posted by Hemanshu Kumar View Post
                            Actually, I think \\ would have worked. But I prefer / because then I don't have to worry about the fact that Stata treats \ as an escape character. And / has the added advantage that it works across different OSes -- macOS, Linux, and Windows as well.

                            The problem in your code was probably because you had a space between \\ and `i',

                            import delimited BACI_HS96_1996-2020-CEPII\\◼︎`i'.csv
                            OK,I get it. Thank you!

                            Comment


                            • #15
                              Absolutely. If I wrote my paths by hand I would always use a forward slash. But, as a rule, I avoid writing paths by hand because it is error prone. I typically copy and paste any path either from the terminal or from the file explorer. In Windows I find it is a little easier to fill in a second backslash than it is to change a backslash to a forward slash. So I use double backslashes, and don't spend much time worrying what will happen if I ever have to move the script over to Linux and run it someday.

                              Comment

                              Working...
                              X