0
votes

I am trying to run the reg and geting back the coefficient values in Stata. I did the following. Assume that y is dependent variable, k,l,m,n are independent variables, and there is a new variable new that I created.

loc vars k l m n
reg y `vars'

# I know that I can get back the coefficients using mat list e(b) but I  need to 
get coefficient of each variable and use it to compute the elasticity (one at a time).

# so, I run the following loop but it doesn't work.



foreach i in vars {
sca coeff`i' = _b[`i'] # main problem here 
sca cons = _b[_cons]  # main problem here
corr new `i' , c # correlation of new with each independent vars
sca cov_`i' = r(cov_12)
sum `i' 
sca elas_`i' = (coeff`i'*r(mean))/10 # elasticity not working 
}

Any help in this regard will be highly appreciated.

1
Are you trying to code one of the elasticity commands available through margins in Stata 12? Have a look, Stata might be already doing what you want.Fr.
@Fr. : Thanks you. Yes, I can also use margins which gives me the same answer.Metrics

1 Answers

1
votes

As Fr. says, you shouldn't need to do this, given margins. But why is your code not working? You are using the wrong syntax for foreach.

You should be typing not

  foreach i in vars

but

  foreach i of local vars

as otherwise Stata will use the literal text vars not the contents of local macro vars. The two syntaxes are explained in the help and at greater length in http://www.stata-journal.com/sjpdf.html?articlenum=pr0005

Smaller points:

  1. The assignment sca cons = _b[_cons] should work but you don't need to repeat it every time round the loop.

  2. You don't show us your code for generating new, so we have to assume that is all right.

By the way, "doesn't work" doesn't mean much. I once compiled a list of about 20 meanings I have encountered, the most important including "is illegal" and "doesn't do what I want". So, giving detail on exactly what happened -- in this case exactly what Stata typed in response -- is always helpful.