Announcement

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

  • Error "file handle already exists" - Dropping file handle

    Hi. I am trying to run the code below, and it worked fine the first time but then began giving me the error "file handle tableA2 already exists r(110);". I tried "macro drop tableA2" and "clear" at the start and end, but that doesn't solve the problem.

    Any help would be much appreciated.




    Code:
    clear
    
    * list of variables
    local outcomeVars "rate_con_avg7day rate_rec_avg7day rate_dec_avg7day"
    
    local sets `" "" "_trend" "'
    
    foreach set of local sets {
        
        
        * open file, write header
        file open tableA2 using "${paperdir}\Tables\EScoeff1`set'.csv", write replace
    
        file write tableA2 "Variable,All states,Early adopters,Late adopters" _n
    
        foreach sample in outcome {
            
            use "${coeffdir}\coefficients.dta", replace
    
            * Average effect of all states
            foreach var of local `sample'Vars {
                
                * ALL
                qui egen wavg_`var' = wtmean(b_`var'_b`set') ///
                    if treated == 1, weight(1/(se_`var'_b`set'^2))
                    *NOTE maybe include cem_varlist==1/2/3/4, so & "cem_varlist == 1"
                qui egen wvar_`var' = mean(1/(se_`var'_b`set'^2)) ///
                    if treated == 1 
                qui replace wvar_`var' = sqrt(1/wvar_`var')
    //             qui egen simplevar_`var' = total(se_`var'_b`set'^2) ///
    //                 if acquired == 1 & same_atc3 == 1 & medVH == 0
    //             qui replace simplevar_`var' = sqrt(simplevar_`var')
                
                * record values
    //             sum b_`var'_b`set' if acquired == 1 & same_atc3 == 1 & medVH == 0
    //             local avgAll : display %4.2f r(mean)
    //             sum simplevar_`var'
    //             local sdAll : display %4.2f r(mean)
    //             drop simplevar_`var'
                sum wavg_`var'
                local avgAllEff : display %4.2f r(mean)
                drop wavg_`var'
                sum wvar_`var'
                local sdAllEff : display %4.2f r(mean)
                drop wvar_`var'        
                
               
               
    
                file write tableA2 "`var',`avgAllStates',`avgEarlyStates',`avgLateStates'" _n
                file write tableA2 `",="(`sdAllStates')",="(`sdEarlyStates')",="(`sdLateStates')""' _n
                
                }
            
            }
    
        file close tableA2
        
    }
    
    clear

  • #2
    I tried "macro drop tableA2" and "clear" at the start and end, but that doesn't solve the problem.
    Makes sense. tableA2 is a file handle, not a macro. I think you need to close the file to free up the handle with:

    Code:
    file close tableA2
    My guess is that the last time you ran this, you never actually reached the -file close tableA2- line, so you need to close the file now before you can use the same handle.

    Comment

    Working...
    X