2015-04-24 56 views
0

我遇到了關於使用Stata進行數據管理的實際問題。我打算做的是創造30個省會城市之間的球形距離變量(因此大約有870個相同的值)。已經有一些用戶編寫的命令來處理這個問題(通過谷歌地圖),但我的問題是,由於某些機密的原因,數據存儲在一個隔離到互聯網的獨立計算機,所以我必須定義所有的一對一的,在do-file中創建一對一的距離值,然後將它們合併到數據中。鑑於非工作量(儘管不是不可行),我想知道是否有一些聰明的方法來完成這項工作。我有一個excel工作表,其中的距離就像一個協方差矩陣,省首府的名字出現在第一行和第二列,就像一個下三角矩陣。代表值如何在stata中巧妙地將協變類錶轉換爲一對一的對?

 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的有益的建議

+0

Can --reshape--有用嗎? – zlqs1985

+0

是的。你試過了嗎?你有什麼問題,如果有的話? –

+0

只能重申:一個標準的'重塑長'應該在這裏工作。如果您遇到問題,請回報。如果是這樣,請告訴我們一些代碼與您的嘗試和問題的明確描述。 –

回答

0

謝謝,我已經部分解決了這個問題,而且我提出我的解決方案中獲益誰可能達到類似的問題,任何新人,並希望從真正的專家利於提高我的代碼

//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" 
····· 
相關問題