Hi,
I have the following states. I want to assign a 2-digit code to each state. The dataex is given below.
To assign stcode to each state (e.g., wanted_stcode variable) I tried using the following loop.
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.
Comment