Announcement

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

  • Error when (re)using temporary saved file for merging

    Hi,

    I have an issue with using temporary saved files. I have saved r1r2 and r3 as temporary file followed by save command as the following:
    tempfile r1r2
    save "`r1r2'"

    When I reuse the files inorder to merge, I get an error message: invalid file specification r(198).

    use `r1r2'
    merge CHILDID using `r3'
    replace inr2=0 if _m==1
    drop _m
    sort childid
    merge childid using `r3'
    replace inr3=0 if _m==1
    drop _m


    I don't see from where the error message arrives.





  • #2
    You don't say where the error message is...is it in use or merge? My guess would be that you need quotes around the tempfile name, like this...

    Code:
    use "`r1r2'"

    Comment


    • #3
      Sorry for not specifying more, the error message arrives when I start to execute use `r1r2' and with quotes doesn't work either.

      Comment


      • #4
        But you didn't tell what the error message says, so you're still asking us to guess what happened. Please post the exact sequence of commands you gave and the exact output (including the error message) that Stata gave you.

        Comment


        • #5
          My codes are :

          global ocr1 C:\Users\thth0019\Box Sync\SurveyData\R1_OC\
          global com_r1 C:\Users\thth0019\Box Sync\SurveyData\R1_OC\community

          global ocr2 C:\Users\thth0019\Box Sync\SurveyData\R2_OC
          global com_r2 C:\Users\thth0019\Box Sync\SurveyData\R2_OC\Community
          global consumption_r2 C:\Users\thth0019\Box Sync\SurveyData\R2_OC\Composite_score

          global ocr3 C:\Users\thth0019\Box Sync\SurveyData\R3_OC
          global com_r3 C:\Users\thth0019\Box Sync\SurveyData\R3_OC\Community
          global composite_r3 C:\Users\thth0019\Box Sync\SurveyData\R3_OC\Composite_score

          global output C:\Users\thth0019\Box Sync\SurveyData
          global output1 C:\Users\thth0019\Box Sync\SurveyData

          use CHILDID using "$ocr1/inchildlevel8yrold.dta",clear
          gen oc =1
          label var oc "Older cohort"
          gen inr1=1
          label var inr1 "Child is present in round 1"
          qui append using "$ocr2/INChildQuest12YrOld.dta"
          replace oc=1 if oc==.
          keep CHILDID oc
          gen inr2=2
          label var inr2 "Child is present in round 2"
          sort CHILDID
          tempfile r1r2
          save "`r1r2'"


          use "$ocr3/in_oc_childlevel.dta",clear
          gen oc = 1
          gen inr3 = 3
          label var inr3 "Child is present in round 3"
          keep CHILDID oc
          sort CHILDID
          tempfile r3
          save "`r3'"

          use "`r1r2'"
          merge CHILDID using "`r3'"
          replace inr2=0 if _m==1
          drop _m
          sort CHILDID
          replace inr3=0 if _m==1
          drop _m

          gen panel123=inr1==1 & inr2==1 & inr3==1
          label var panel123 "Child is present in all rounds"

          sort CHILDID
          tempfile panel
          save `panel'


          . use "`r1r2'"
          invalid file specification
          r(198);

          however, when I come to third part where I wish to merge the files: I get the error message. I have executed the codes with and without quotes signes.

          Comment


          • #6
            So, it seems that you are having no difficulties accessing `r1r2' at first, but when you get to the -use "`r1r2'"- command that precedes the -merge- command, Stata balks, even though it has previously both -use-d and -save-d `r1r2'.

            The use of quotes around `r1r2' is not necessary. The actual filenames Stata generates with the -tempfile- command do not contain embedded blanks and can be used without quotes. (It is wise to put quotes around them anyway when doing a -save- command to avoid inadvertently overwriting some other file if you mistype the name of the tempfile, but that is a separate issue.)

            There is no obvious problem with your code. Here's what I think is happening.

            I think you are running this code in separate sections. So perhaps you have highlighted the first several paragraphs and then run them. Then you go back to your do-file and separately highlight the paragraph that is giving you the error message. And Stata no longer remembers what `r1r2' is. This is because local macros defined during a highlighted-executed block of commands go out of scope after that block of commands terminates. So you define r1r2 the first time around. But r1r2 goes out of existence at the end of that block. When you come back later with -use `r1r2'-, r1r2 no longer exists, and Stata thinks your command is just -use-, which is a syntax error. The solution is simple: run the entire do file all at once without interruption--the local macros will remain in existence throughout and you shouldn't encounter any problem.

            Comment

            Working...
            X