Announcement

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

  • Encode and generate with different starting number than 1

    Dear colleagues,

    I am trying to encode and generate a list of 30 villages. I know encode and generate will convert the variable into numeric with value labels of village names in alphanumeric order.

    I would like to keep that but I would like that the encode starts the numbering from 901 instead of 1. I don't want to manually add 900 onto the encoded variables because this will remove my value labels and I will have to manually add it.

    Anyone knows how to change the starting number for encode, gen?

    Thanks,
    Last edited by Kamran Khan Niazi; 07 Oct 2024, 05:02. Reason: encode

  • #2
    You could do it the slow way of defining 901 to 930 as numeric identifiers and then value labels for those values.

    This is a little more systematic. labmask is from the Stata Journal.

    Code:
    . clear 
    
    . 
    . set obs 26 
    Number of observations (_N) was 0, now 26.
    
    . 
    . gen village_name = word("`c(ALPHA)'", _n)
    
    . 
    . list if inrange(_n, 1, 5) | inrange(_n, 22, 26)
    
         +----------+
         | villag~e |
         |----------|
      1. |        A |
      2. |        B |
      3. |        C |
      4. |        D |
      5. |        E |
         |----------|
     22. |        V |
     23. |        W |
     24. |        X |
     25. |        Y |
     26. |        Z |
         +----------+
    
    . 
    . * you start here with your own variable name 
    . sort village_name 
    
    . 
    . gen village_id = 900 + sum(village_name != village_name[_n-1])
    
    . 
    . labmask village_id, values(village_name)
    
    . 
    . list if inlist(_n, 1, 5) | inlist(_n, 22, 26)
    
         +---------------------+
         | villag~e   villag~d |
         |---------------------|
      1. |        A          A |
      5. |        E          E |
     22. |        V          V |
     26. |        Z          Z |
         +---------------------+
    
    . 
    . list if inlist(_n, 1, 5) | inlist(_n, 22, 26), nolabel 
    
         +---------------------+
         | villag~e   villag~d |
         |---------------------|
      1. |        A        901 |
      5. |        E        905 |
     22. |        V        922 |
     26. |        Z        926 |
         +---------------------+

    Comment


    • #3
      Here are two other canned approaches:

      Code:
      label define village_label, // <- define an empty value label
      encodelabel village_string , generate(village_numeric) label(village_label) min(901)
      or
      Code:
      encode village_string , generate(village_numeric)
      elabel adjust : replace village_numeric = village_numeric + 900
      Both encodelabel and elabel are from SSC.
      Last edited by daniel klein; 07 Oct 2024, 07:24.

      Comment


      • #4
        Super stuff, daniel klein. Thank you.

        Comment

        Working...
        X