Hi everyone

Stata 16
I run capture regress and have an if-else command to handle errors.
The data set contains multiple survey years and I want to try to regress each year separately. Most years are fine but in the year 2000 there was no survey so it should enter the if command and skip the else part.
I've checked the code and it does execute the if part but also enters the else part - wrongly. Ive tried to change the else to if _rc==0 and this still occurs.

The error is (unsurprisingly):
_b not allowed when e(b) is not present

Here's a sample:

Code:
forvalues year = 1993/2009 {
     cap reg `q' treat `controls' if survey_year==`year', robust
      
      if _rc!=0 {
             (handle errors)
             di "No survey this year"
       }

      di _rc
      else {
            local beta = _b[treat]
       }
}