import excel "K:\计量作业\科技金融空间计量\01矩阵.xlsx", sheet("Sheet1") firstrow clear
encode 地区,generate(id)
sort id
drop 地区 id
xpose, varname clear
sort _varname
order _varname
levelsof _varname,local(name)
local i=1
foreach w in `name' {
rename v`i' `w'
local i=`i'+1
}
save 01矩阵.dta
To create the file listed below, we typed
. spmatrix create idistance Idist
. spmatrix export Idist using small.txt
(matrix Idist saved in file small.txt)
We did this after keeping four observations so that we would have a small file to show you.
The resulting file is
. type small.txt
4
20029 0 .225898983673981 .259698923068494 .746562405514367
33003 .225898983673981 0 .123515701241913 .187089086384635
41021 .259698923068494 .123515701241913 0 .264715523882705
48227 .746562405514367 .187089086384635 .264715523882705 0
The file records a 4 × 4 spatial weighting matrix. Real examples would record much larger matrices.
N × N matrices are recorded in N + 1 lines.
The first line states that N = 4. The matrix is 4 × 4.
The second and subsequent lines each record N + 1 values with spaces between them. The first
value, 20029, is the ID (FIPS) value corresponding to the first row of the weighting matrix. The
remaining N values on the line are the first row of the matrix.
The remaining lines are repeats for the second row, third row, and so on. The first value is an
ID value and the rest are that ID’s row of the matrix.
It is a simple and easy-to-read file.