Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using loop to create state-codes for states

    Hi,

    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
    To assign stcode to each state (e.g., wanted_stcode variable) I tried using the following loop.

    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.


  • #2
    You can accomplish this by creating that lookup table that you show at first, and then -merge 1:m state using <main dataset>, keep(match using)-. If you have recent releases of Stata, then you can do the same thing with frames. A more roundabout approach could involve -encode- with a defined set of value labels of the states' full names, redefine that set of value labels with the definitions, and then -decode-.

    Comment


    • #3
      I agree with Joseph that it is probably better to create a small dataset with only full name and short name, and then to merge it back into the whole dataset.

      Otherwise what you re probably trying to do can be done like this:

      Code:
      . levelsof state, local(statefullname)
      `"Andhra Pradesh"' `"D & N Haveli"' `"Daman & Diu"' `"Himachal Pradesh"' `"Jammu & Kashmir"' `"Maharastra"
      > ' `"Punjab"'
      
      .  local stateshortname "AP DN DD HP JK MH PB"
      
      . local numstates: word count `statefullname'
      
      . gen str stcode = ""
      (7 missing values generated)
      
      . forvalues i=1/`numstates' {
        2. local a: word `i'  of `statefullname'
        3. local b: word `i'  of `stateshortname'
        4. replace stcode = "`b'" if state=="`a'"
        5. }
      variable stcode was str1 now str2
      (1 real change made)
      (1 real change made)
      (1 real change made)
      (1 real change made)
      (1 real change made)
      (1 real change made)
      (1 real change made)
      
      . list, sep(0)
      
           +--------------------------------------+
           |            state   wanted~e   stcode |
           |--------------------------------------|
        1. |  Jammu & Kashmir         JK       JK |
        2. | Himachal Pradesh         HP       HP |
        3. |           Punjab         PB       PB |
        4. |      Daman & Diu         DD       DD |
        5. |     D & N Haveli         DN       DN |
        6. |       Maharastra         MH       MH |
        7. |   Andhra Pradesh         AP       AP |
           +--------------------------------------+
      
      .

      Comment


      • #4

        Thank you for the code and suggestion, Joro Kolev. That worked fine with me.

        Comment


        • #5
          Thank you, Joseph Coveney, for the suggestion.

          Comment

          Working...
          X