Announcement

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

  • For each code Error

    Hello



    I am trying to run the following code in Stata but getting the error of invalid file specification.

    Thanks


    clear
    foreach var in 1906 1907 1908 {
    cd "C:\Users\ramzan\Downloads\Compressed\air_temp_201 4"
    import delimited C:\Users\ramzan\Downloads\Compressed\air_temp_2014 \air_temp.`var'
    export excel using air_temp.`var'.xlsx, replace
    save

    }





  • #2
    When a filename ends in .whatever, Stata assumes that the . separates the filename from the filename extension, and so does not add a default extension (which would be .csv in the case of -import delimited-). So Stata thinks you are asking it to read in a file C:\Users\ramzan\Downloads\Compressed\air_temp_2014 \air_temp.1906, and, finding no such file, it halts and complains. What you need is
    Code:
    import delimited C:\Users\ramzan\Downloads\Compressed\air_temp_2014 \air_temp.`var'.csv
    Actually, this is one reason why it is best not to use . as a character in a filename. Use of underscore (_) characters to separate "words" in a filename incurs no problems.

    Comment


    • #3
      Is there a spurious embedded space in the directory name between 1 and 4?

      Comment


      • #4
        Originally posted by Daniel Feenberg View Post
        Is there a spurious embedded space in the directory name between 1 and 4?
        No please

        Comment


        • #5
          Actually Dan Feenberg raises a good point that I missed in my response at #3. Your code:
          Code:
          cd "C:\Users\ramzan\Downloads\Compressed\air_temp_201 4"
          import delimited C:\Users\ramzan\Downloads\Compressed\air_temp_2014 \air_temp.`var'
          appears to contradict itself. In the-cd- command, the name includes a space between 201 and 4, but in the -import delimited- command you have 2014 with no embedded space. Unless you have to separate directories, one named "air_temp_2014" and another named "air_temp_201 4", one of these is incorrect, and whichever command has the incorrect directory name will throw an error message.

          Note: Even if it turns out that it is the -cd- command that is wrong, you still need to fix the problem about the filename extension in the -import delimited- command that I pointed out in #2.

          Comment


          • #6
            Thanks

            The following code have worked fine



            foreach var in 2009 2010 2011 {
            clear
            cd "C:\Users\ramzan\Downloads\Compressed\air_temp_201 4"
            import delimited using C:\Users\ramzan\Downloads\Compressed\air_temp_2014 \air_temp.`var'
            save air_temp.`var'.dta , replace

            }


            Comment


            • #7
              Thanks Clyde Schechter and Daniel Feenberg

              Comment


              • #8
                Originally posted by Muhammad Ramzan View Post
                Thanks

                The following code have worked fine



                foreach var in 2009 2010 2011 {
                clear
                cd "C:\Users\ramzan\Downloads\Compressed\air_temp_201 4"
                import delimited using C:\Users\ramzan\Downloads\Compressed\air_temp_2014 \air_temp.`var'
                save air_temp.`var'.dta , replace

                }

                There appears to be a space in 2014 but there is not.

                cd "C:\Users\ramzan\Downloads\Compressed\air_temp_201 4"

                Comment

                Working...
                X