我的數據集中有一個變量countrycode
,它使用ISO數字代碼(基本上是3個數字)來標識每個觀察來自哪個國家。但是,要與其他數據集合並,我需要將這些國家代碼更改爲ISO Alpha值(3個字母)。使用本地Stata來更改變量的值
我有一個csv文件(ISOcodes.csv)匹配的數字代碼和alpha代碼;它看起來像這樣:
num,name,alpha
004,Afghanistan,AFG
248,Åland Islands,ALA
008,Albania,ALB
...
這是我嘗試的解決方案:
insheet using ISOcodes.csv
* Create variables ISOnum_1, ISOnum_2, etc. and ISOalpha_1, ISOalpha_2, etc.
* ISOnum_1 and ISOalpha_1 should refer to the same country
local N = _N
forvalues x = 1 (1) `N' {
local ISOnum_`x' = num[`x']
local ISOalpha_`x' = num[`x']
}
clear
insheet using maindataset.csv
* Replace all numeric values in `countrycode` with the corresponding `alpha` values
recast countrycode str3
foreach x = 1 (1) `N' {
replace countrycode = ISOalpha_`x' if countrycode == ISOnum_`x'
}
list countrycode
不過,我還沒有能夠創造當地人;當我運行這個時,只要我嘗試分配第一個本地時,就會收到錯誤「num not allowed」。關於這裏出了什麼問題的任何想法?我在下一節中會遇到任何問題?我對Stata非常陌生。
此外,當我清除ISOcodes.csv數據集以導入我的主數據集時,是否會丟失我剛分配的所有本地數據?
交叉發佈在statalist.org/forums/forum/general-stata-discussion/general/...在其他論壇上交叉發佈並非是無序的,但是可以有禮貌地告訴我們。見例如http://meta.stackexchange.com/questions/141823/why-is-cross-posting-wrong-on-an-external-site進行討論。統計專家對此相反明確。 – 2014-12-08 11:36:54