桃子桃子酱 发表于 2018-9-18 10:12 
谢谢大神,但我用这个代码跑出来不是我想要的结果。我想要的是在原来的数据里加入新生成的系数变量,这个 ...
set more off
use "D:\file1.dta",clear
matrix tbl = J(2, 2, 0) // collect results to a matrix
matrix colnames tbl = id b // name the rows
matrix rownames tbl = 1 2 // name the column of the matrix
forv i=1/2{
qui reg y x if id==`i', noconstant
matrix tbl[`i', 1] = `i'
matrix tbl[`i', 2] = _b[x]
}
matrix list tbl // show the results
clear
svmat tbl, names(b) // use col names as variable names
rename b1 id
rename b2 b
save "D:\file2.dta",replace
use "D:\file1.dta",clear
merge m:1 id using "D:\file2.dta"
drop _merge