我在使用 Stata 进行数据管理时遇到了一个实际问题。我计划做的是在中国的30个省会城市(因此大约有870个相同的值)之间创建球形距离变量。有一些用户编写的命令来处理这个问题(通过谷歌地图),但我的问题是,出于某种机密原因,数据存储在与互联网断开连接的隔离计算机中,所以我必须在do-file中定义所有一对一的距离值,然后将它们合并到数据中。鉴于无可匹敌的工作量(尽管并非真的不可行),我想知道是否有一些聪明的方法来完成这项工作。我有一个 excel 工作表,其中的距离就像一个协方差 martrix,省会城市的名称出现在第一行和第一列中,它就像一个下三角形矩阵,.代表价值观
A B C D E AD
capital_1 capital_2 capital_3 capital_4 ······ capital_30
1 capital_1
2 capital_2 '
3 capital_3 ' '
4 capital_4 ' ' '
········
capital_30 ' ' ' '
我知道如何导入这样的martrix,但是我可以生成所需的一对一对吗?谢谢。
感谢您@Roberto的有用建议,我已经部分解决了这个问题,我提出我的解决方案以使任何可能遇到类似问题的新人受益,并希望从真正的专家中受益以改进我的代码
//import data from excel file
import excel using distance.xls
*********************************
* rename the variable name
* (to make it more well orgnized to facilitie use of --reshape-- command
*********************************
* rename the first column
ren A id
* rename the following variables
local i=0
foreach var of varlist B-AF {
local j=`i'+1
local i=`i'+1
ren `var' distance`j'
}
*********************************
* reshape the data
*********************************
reshape long distance,i(id) j(city)
tostring city,replace
***the following part is really urgly, because I don't know how
*to gen a one-to-one mapping between orginal province name and
*the new generated variable name like distance`j',I have to recover
*them by hand, I hope someone can help me to improve this part**
replace city="北京" if city=="1"
replace city="天津" if city=="2"
replace city="河北" if city=="3"
replace city="山西" if city=="4"
replace city="内蒙古" if city=="5"
replace city="辽宁" if city=="6"
replace city="吉林" if city=="7"
·····