Hi, I am trying the 'foreach' command for the first time and cannot figure out the problem in my code. Request you help.

I have a panel variable - company name and I am trying to run repeated regressions by company and store residuals after each regression

I have written the following code:
Code:
  generate rebit = .
  foreach conm i = 1/73 {
   capture reg LnEBIT time if conm ==`i'
   capture predict temp, resid
     capture replace rebit = temp if conm ==`i'
   capture drop temp
   }
conm - company name
number of companies = 73
So the loop will have to run 73 times and after each regression should store the residuals for that company regression.

But when I run this code, Stata gives me the following error:
invalid syntax
r(198);

Could you please let me know where I am going wrong and how to correct this code?


Thank you for your help and advice.