Announcement

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

  • Error importing different csv files to Stata

    Hello,

    I am trying to import different csv files in Stata with a loop. I am using the following code:

    local countries "BEN CIV CMR COD GHA GIN KEN LBR MLI MOZ MRT NER NGA SEN SLE TGO TZA UGA ZMB"

    foreach c of local countries {

    clear all
    import delimited "$agromaps"`c'".DBF.csv", varnames(1)

    drop comments
    keep if element_co == 51
    drop element_co

    merge m:1 item_code using "$agromaps\FAO_ITEM_CODES.dta"
    drop if _merge == 2
    drop _merge
    keep if item_code == 661 | item_code == 656 | item_code == 328 | item_code == 254 | item_code == 242

    sort name1 name2 item_code year
    bysort name2 item_code: egen avg_crop = mean(data)

    save "$agromaps"`c'"_DBF.dta", replace

    }

    This code works when I just do by one file, for example BEN. However, when I wrote the code I mentioned, it is said "using required" which I cannot understand why. Any help?

  • #2
    Your construction -"$agromaps"`c'".DBF.csv"- confuses the parser because it misuses quotes. Look at this
    Code:
    . global agromaps C:/Users/Clyde/Sandbox
    
    . local c BEN
    
    . display `""$agromaps"`c'".DBF.csv""'
    "C:/Users/Clyde/Sandbox"BEN".DBF.csv"
    Notice those quotes around BEN, which are not legal in a filename. So the parser doesn't know what to do with this.

    Use -"${agromaps}`c'.DBF.csv"- instead.
    Code:
    . global agromaps C:/Users/Clyde/Sandbox
    
    . local c BEN
    
    . display `""${agromaps}`c'.DBF.csv""'
    "C:/Users/Clyde/SandboxBEN.DBF.csv"


    Comment


    • #3
      Hello Clyde,

      Thank you so much for your answer.

      I have tried what you suggested: `""${agromaps}`c'.DBF.csv""' or "${agromaps}`c'.DBF.csv and they are not working.

      However, it is giving me a different error than before: file C:\Users\malorico\Dropbox\JMP\AgroMapsBEN.DBF.csv" not found

      Comment


      • #4
        In the directory C:\Users\malorico\Dropbox\JMP is there a file named AgroMapsBEN.DBF.csv?

        Comment


        • #5
          Sorry, I missed the trailing quote. I think you want to use
          Code:
          "${agromaps}`c'.DBF.csv"

          Comment


          • #6
            No, there is a file called BEN.DBF within the AgroMaps folder.

            Comment


            • #7
              I tried Nils' suggestion: "${agromaps}`c'.DBF.csv", but it is giving the same error file C:\Users\malorico\Dropbox\JMP\AgroMapsBEN.DBF.csv not found

              It is super strange.

              Comment


              • #8
                Ah, so AgroMaps is a folder? And does it contain a file named BEN.DBF, as you said, or BEN.DBF.csv? If the latter, then you want to add a trailing slash to wherever you define $agromaps. Or add a slash into the file path:
                Code:
                "${agromaps}\`c'.DBF.csv"

                Comment


                • #9
                  -"${agromaps}\`c'.DBF.csv"- will not work either, and will trigger yet a different error message. That's because the sequence \` will not be understood as a path separator followed by an opening macro quote. It will be interpreted as an escape sequence: a literal backtick (`) character that does not initiate a local macro. Consequently `c' will never be interpreted and an illegal file name will be the result.

                  Try -"${agromaps}/`c'.DBF.csv"-. The /` sequence is not an escape sequence and will be correctly interpreted as / followed by ` initiating a local macro. And / is an acceptable path separator in Windows. (Also, in I/O commands, Stata for Windows substitutes \ for / anyway, but it does so after macro expansion is done, so you don't end up back in the escape sequence problem.)

                  Comment


                  • #10
                    It is working with the /. Thank you so much, Clyde and Nils.

                    Just a question for curiosity and for learning, why I have to put agropmaps between {} ?

                    Comment


                    • #11
                      why I have to put agropmaps between {} ?
                      ... so that Stata knows where the name of the global macro stops.

                      See [U] 18.3.10 in the PDF documentation, aka the manual.

                      Comment


                      • #12
                        Thank you, Nick! I did not know that.

                        Comment


                        • #13
                          Except that you don't really need it -- given that using a forward slash is needed. Stata knows that a macro name can't include a forward slash, so this works:

                          Code:
                          . global agromaps foo
                          
                          . di "$agromaps/bar"
                          foo/bar

                          Comment

                          Working...
                          X