I have the following states. I want to assign a 2-digit code to each state. The dataex is given below.
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str16 state str2 wanted_stcode
"Jammu & Kashmir" "JK"
"Himachal Pradesh" "HP"
"Punjab" "PB"
"Daman & Diu" "DD"
"D & N Haveli" "DN"
"Maharastra" "MH"
"Andhra Pradesh" "AP"
end
Code:
gen stcode1 = ""
egen stateid = group(state), label
su stateid, meanonly
local vars "JK HP PB DD DN MH AP"
foreach j of local vars{
forvalues i = 1/`r(N)'{
replace stcode = "`j'" if state == "`i'"
}
}
I would appreciate any help in this regard.
0 Response to Using loop to create state-codes for states
Post a Comment