Announcement

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

  • Invalid file specification

    Hello,

    My coding (after preceding cleaning lines) is as follows and it gives "invalid file specification" error:
    Code:
    forval n=2(1)33{
    
    *codes for importing and cleaning data
    
     tempfile teacher_`n'
    save `teacher_`n'', replace
    }
    
    
    use `teacher_2'
    forval n=3(1)33{
    append using `teacher_`n''
    }
    While the same code worked for another file, I have difficulty here.

    I checked the other Posts asking about the similar error but couldn't find a proper solution for my case.

    Thank you!
    Last edited by Esma Ozer; 24 Jul 2020, 16:43.

  • #2
    You need to put the whole code and all the error messages Stata returned, otherwise hard to know what is going on.

    What might be going on here is that the tempfile is visible only within the first loop, and when you try to call it in the second loop it is not recognised anymore. (I do not remember by heart the rules according to which temp-things operate, I figure them out by trial and error.)

    Comment


    • #3
      There is nothing obviously wrong with the code you show. Here's my guess what's going wrong: this code can only work if it is run without interruption from beginning to end. If you are running part of the code, stopping, and then running the rest (e.g. running the importing and cleaning code as one batch, and then running the appending loop) it will fail because when you interrupt the code, the local macros (tempfile names are local macros) defined at the top cease to exist, so when you try to -use `teacher_2'- that translates to just -use- with no filename at all. So try the code again and make sure to run it all in one fell swoop. If that still fails, post back, and also show an exact copy of what Stata gives you in the Results window so everyone can see exactly where Stata is finding a problem.

      Comment


      • #4
        set trace on and re-run. At least you will know where it bumps the wall.

        Most likely the problem is in the other lines that you've omitted (if any). You will get this error if the second block in in a different procedure, (for a wild guess).

        PS: normally you don't save with replace option after obtaining a new tempfile name, if you trust Stata can generate new temporary names.

        Comment


        • #5
          Hello, again
          thank you all for your response, they are helpful.

          While I have been able to run the code by changing some small typos in my do-file and run it all without interruption; for another file, I still have the same problem "invalid file specification". After the following codes;
          Code:
          forval n=1(2)65{
          
          import excel "$data/`n'_XXX.xlsx", sheet("corrected") firstrow clear
          
          drop in 1
          drop in 1
          drop in 1
          
          replace AB= AB*2
          replace AC =AC*3
          replace AD= AD*4
          replace AE= AE*5
          replace AF=AF*6
          replace AG = AG*8
          replace AH = AH*10
          egen total_teacher = rowtotal(AA-AH)
          
          *There are additional data cleaning lines, running without any problem.
          
          save "XXX/`n'_test.dta", replace
          
          tempfile teacher_`n'
          save `teacher_`n'', replace
          }
          
          use `teacher_1'        
          forval n=3(2)65{
          append using `teacher`n''
          }
          It gives the error:
          Code:
          .
          use `teacher_1'        
          
          . forval n=3(2)65{
            2. append using `teacher`n''
            3. }
          invalid file specification
          r(198);
          Seemingly, the problem that I can't notice is with the "append" line..


          Comment


          • #6
            In this case, you have a coding error. In the first loop, you save a tempfile called teacher_`n'. But in the second loop you attempt to append `teacher`n''. Note that you omitted the underscore (_) between teacher and `n' there. So `teacher`n'' doesn't exist. Put in the underscore and it will run without error messages.

            Comment


            • #7
              Thank you so much, dear Clyde.

              I should have noticed this small typo, indeed...
              Now, it works well.

              Comment

              Working...
              X