Announcement

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

  • Problems with the file read command

    I have a list in txt format (list.txt) and I want Stata to read the list one line at a time (each line is the name of a dataset in ASCII format that I want to open and save as dta). My code is:
    Code:
     file open myfile using "list.txt", read
      file read myfile line
      insheet using `line', delimiter(";")
    But Stata keeps returning the error "invalid file specification". I tried display `line' and line is apparently empty... However, I tried

    Code:
    file read myfile line
    once more and display shows me that Stata is reading the second line and insheet works with no errors.... Any ideas as to why Stata is reading my first line as if it were blank?



  • #2
    You are not giving us all the details we need to understand exactly what you are doing, but it seems possible to me that you are defining some of your code in one place (most likely, a do-file editor window) and some in another place (e.g. the main command window). If so, then the local macro will be not visible outside where it is defined. That's what local means....

    A different problem, which perhaps hasn't bitten you yet, or is covered by code you haven't shown us, is that you need to have a loop over the lines of the file.

    Comment


    • #3
      Sorry about not giving all the details! I'm new to the forum and didn't want to be prolix... My full code is:

      Code:
        file open myfile using "list.txt", read
        file read myfile line
        insheet using `line', delimiter(";") 
        save `line'.dta, replace
        save master_data.dta, replace
        file read myfile line
        while r(eof)==0 {
            insheet using `line', delimiter(;) no names
            save `line'.dta, replace
            append using master_data.dta
            save master_data.dta, replace
            drop
            file read myfile line
        }
      I basically copied the code given at http://www.ats.ucla.edu/stat/stata/f...many_files.htm

      Comment


      • #4
        After converting the files to dta I want to combine them all together as well....

        Comment


        • #5
          Maria,

          The only thing wrong with your code is the extra space in the middle of the nonames option, which would cause a different error. Otherwise, I don't see anything wrong with your code and I couldn't reproduce your problem, so perhaps the problem is with your file (list.txt). If you are not at liberty to share the file with us, one option would be to precede the first insheet command with capture which will ignore problems with bad filenames and proceed to the next file.

          Incidentally, along the same lines, here is a way (UCLA notwithstanding) that I think will work to simplify your code a bit:

          Code:
            
           file open myfile using "list.txt", read  
           while r(eof)==0 {      
               file read myfile line
               insheet using `line', delimiter(;) nonames
               capture append using master_data.dta
               save master_data.dta, replace
               drop
                file read myfile line  
          }
          Regards,
          Joe

          Comment


          • #6
            An extra detail is that your code presumes no spaces within filenames. If you have such spaces, you will need to include the filenames within " ".

            Comment


            • #7
              Nick and Joe,

              Sorry for the tardy response. I couldn't get back to Stata until today. Nick, you were right about my mixing up where I defined my local macro. When I gave up on using a do-file, and typed everything on the command window it worked (although I still don't understand what was happening before because I'm sure I defined everything on the do-file and then simply ran it...). Thank you very much for your help. Just started learning Stata two weeks to build up a dataset, and it has been I tough journey since then... But enjoying it so far! Thanks again.

              Regards,

              Maria

              Comment

              Working...
              X