# Creating variables from saved coefficients

Hi all, Say I run 10 OLS simple regressions in a do-loop and I save the intercept and the slope of each regression with the COEF option. Been going crazy about this: How can I put the 10 intercepts saved into one vector (variable) and the 10 slopes saved into another vector (variable)??? Thanks in advance!

edit retag close merge delete

Sort by » oldest newest most voted

This can be done simply by DIMensioning the two vectors, saving the coefficient vector on each OLS and then using the COPY command to copy the coefficients to the locations in the vectors you desire.

Here is an example of doing this with some fake generated data.

* Disable detailed output information
set nodoecho
set noecho

* Dimension the output vectors to store the intercepts and coefficients
dim myinter 10
dim mycoeff 10

* Set our sample to 12 observations
sample 1 12

* Do 10 fake regressions and copy the results to a single vector
*   N.B. Prefixing commands by ? hides their output
do(% = 1, 10)

* Generate 10 fake data sets for variables y and x
?genr y% = % + nor(1)
?genr x% = %-1 + 10 * nor(5)

* Perform a simple regression of y on x and a constant - save the coefficients
?ols y% x% / coeff=coeff%
print coeff%

* Copy the generated coefficient to the place in the vector we wish
copy coeff% myinter / frow=1;1 trow=%;%
copy coeff% mycoeff / frow=2;2 trow=%;%

endo

* Print the vectors
print myinter mycoeff

more