Announcement

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

  • #16
    The code you show in #15 is convoluted and overly complex, but it should work. I can see only one reason why it would work properly with a global macro but not a local. Code containing local macros must be run uninterrupted. If you stop the code, the local macros go out of existence, and are not re-established when you resume it. So to use the code with local macros you must run it all in one fell swoop.

    That said, have you tried the code in #14? It is simpler and it should do the job for you.

    Comment


    • #17
      Dear all, I had to do the same thing, using the code in #14 in this form:

      Code:
      clear all
      cd "C:\Users\Siddharth\Documents\Daksh\Data\From_TNT\Case\CSV"
      
      local filenames: dir . files "*.csv"
      
      foreach file of local filenames {
          import delimited "`file'"
          append using temp
          save temp, replace
      }
      but I got the following error:

      Code:
      . 
      . local filenames: dir . files "*.csv"
      
      . 
      . foreach file of local filenames {
        2.     import delimited "`file'"
        3.     append using temp
        4.     save temp, replace
        5. }
      (86 vars, 33178 obs)file temp not found
      r(601);
      
      end of do-file
      
      r(601);
      I'm new to macros and loops, and don't understand them 100%. I'd be very grateful for any help with this !

      Comment


      • #18
        I also tried this, which didn't work either:

        Code:
        cd "C:\Users\Siddharth\Documents\Daksh\Data\From_TNT\Case\CSV"
        clear
        local csvfiles: dir . files "*.csv"
        foreach file of local csvfiles {
         preserve
         import delimited “`file'”, clear
         save temp, replace
         restore
         append using temp
        }
        rm temp // remove temporary .dta file
        save alldata, replace
        This resulted in the error:

        Code:
        . clear
        
        . local csvfiles: dir . files "*.csv"
        
        . foreach file of local csvfiles {
          2.  preserve
          3.  import delimited “`file'”, clear
          4.  ** add syntax here to run on each file**
        .  save temp, replace
          5.  restore
          6.  append using temp
          7. }
        file “ecourt_districtdatacaseinfo104_1_district_and_sessions_judge_new_delhi__phc.csv” not found
        r(601);
        
        end of do-file
        
        r(601);
        Note that the filename shown in the error message is all lower case, but the filenames for the csvs actually have many upper case characters. There are 167 files in total.

        Comment


        • #19
          You may have two different problems here. With regard to your first question, for this code to run, the file temp.dta (which, despite the name is a permanent, not a temporary, file) must already exist when you enter the loop. That will only happen if you create it:

          Code:
          clear all
          cd "C:\Users\Siddharth\Documents\Daksh\Data\From_TNT\Case\CSV"
          save temp, replace emptyok
          
          local filenames: dir . files "*.csv"
          
          foreach file of local filenames {
              import delimited "`file'"
              append using temp
              save temp, replace
          }
          Concerning your last problem, I see two issues. The first is that the “ and ” (so-called smart-quotes, often supplied automatically in word processing programs) are not recognized as quotes in Stata. You must replace them by the ordinary quotation mark character, ", in your code.

          File names are not case sensitive in WIndows, but they are case sensitive in Mac and other operating systems. Nevertheless, if you are running on a non-Windows system, the -local csvfiles: dir...- command will create the macro csvfiles with the appropriate mix of upper and lower case letters that respects what is in your file system. So this should not be an issue. I think the problem is coming from those "smart quotes."

          Comment


          • #20
            Clyde, thank you once again for your help. I ran the second one after fixing the quotation marks and it works fine, except for some problems inherent in the data (string-numeric mismatches etc.)

            Comment


            • #21
              I'm having an issue trying to turn multiple csv files into dta files. I'm running a version of the code used in post #1
              Code:
               clear
              local myfilelist : dir . files"*.csv"
              foreach file of local myfilelist {
              drop _all
              import delimited using `file'
              local outfile = subinstr("`file'",".csv","",.)
              save "`outfile'", replace
              }
              But when I do, I'm getting this error message: (encoding automatically selected: ISO-8859-1)
              (21 vars, 491,032 obs)
              (file 1990.dta not found)
              file 1990.dta could not be opened
              r(603);

              I'm sure these are connected and I'm just missing how, but there are two odd things: 1. I can't find any of the files that I thought were being successfully turned into and saved as .dta files, and 2. the data from 1990 opens automatically in stata after I run this code, so I'm not sure why I'm getting an error saying that it can't be found or opened.

              Comment


              • #22
                All of the messages you are getting are informational, not error messages, except for
                file 1990.dta could not be opened
                r(603);
                As for that one, the first thing that comes to my mind is to question whether you have write privileges in the directory you are working in. It is the same directory that holds the .csv files that are your source data. Perhaps you are only allowed to read there.

                As for "the data from 1990 opens automatically in stata after I run this code," you are misinterpreting what you see. The code has gone through the loop once and created the data that you want to save as 1990.dta. But the -save- command itself fails. Stata halts with the error message you got. When this happens, Stata still contains the data that you wanted to write into 1990.dta. It's just sitting there. File 1990.dta has not been opened. In fact, it does not even exist. You're just looking at what was left over in memory when Stata halted.

                Comment

                Working...
                X