Announcement

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

  • generating id within sub-group


    dear statalist,
    I am faced with another challenge. after exploring all possibility to overcome it but to no avail, I resolve to share it on this forum.
    below is the snapshot of my dataset.
    my task is to generate a sequential unique id for the locality. the id starts and ends within a district. for example , distric kunini in province Karim has three localities and will be assigned id 1, 2 and 3, other locality will be coded the same way. I try to use
    Code:
     group(district locality)
    but end with id generated from first to the last locality instead of breaking it by district. I also think of using
    Code:
    fill (locality)
    which throw an error because locality is not a
    Code:
     numlist
    . I have a large dataset that contains over ten thousand localities spread across over hundreds district. I ran short of option any help will be highly appreciated.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 province str10 district str12 locality
    "Karim"   "kunin"      "kunin"      
    "Karim"   "kunin"      "tafida"     
    "Karim"   "kunin"      "hamma"      
    "Karim"   "abbare"     "yitti"      
    "Karim"   "abbare"     "donadda"    
    "gassol"  "gassol"     "bajumba"    
    "gassol"  "gassol"     "doubeli"    
    "gassol"  "tutare"     "tutare"     
    "gassol"  "tutare"     "gunduma"    
    "gassol"  "mutum biyu" "namnai"     
    "gassol"  "mutum biyu" "garin magaji"
    "gassol"  "mutum biyu" "nanido"     
    "gassol"  "mutum biyu" "agure"      
    "jalingo" "kona"       "mayo dasa"  
    "jalingo" "kona"       "jauro nokiya"
    "jalingo" "kona"       "jekunoho"   
    "jalingo" "kona"       "garu"       
    "jalingo" "jalingo"    "wuro sambe" 
    "jalingo" "jalingo"    "magami"     
    "jalingo" "jalingo"    "mayo gwoi"  
    "jalingo" "jalingo"    "sintali"    
    "jalingo" "jalingo"    "bashin"     
    "yorro"   "kpantisawa" "kpantisawa" 
    "yorro"   "kpantisawa" "kassa"      
    "yorro"   "kpantisawa" "bille"      
    "yorro"   "kpantisawa" "zokwa"      
    "yorro"   "pupule"     "kwaji"      
    "yorro"   "pupule"     "tula"       
    "yorro"   "pupule"     "bajumba"    
    "yorro"   "pupule"     "mika"       
    end
    Kind Regadrs

  • #2

    Code:
    byso province district: gen id=(_n)
    Roman

    Comment


    • #3
      Mutanen:
      you may want to try something along the following lines:
      Code:
      bysort province (district): gen flag=_n
      PS: cross-posted with Roman's similar reply.
      Kind regards,
      Carlo
      (StataNow 18.5)

      Comment


      • #4
        Looking at both Roman's and Carlo's replies, I think you might want your to assign locality IDs in alphabetical order of locality names, so I would recommend
        Code:
        isid province district locality
        bysort province district (locality): generate id = _n
        The reason for the isid command is to check to make sure that no province/district/locality combination appears more than once in your dataset. It will make an error message and stop if there is a duplicate.
        Code:
        . isid province district locality
        
        . bysort province district (locality): generate id = _n
        
        . list, noobs sepby(district)
        
          +-------------------------------------------+
          | province     district       locality   id |
          |-------------------------------------------|
          |    Karim       abbare        donadda    1 |
          |    Karim       abbare          yitti    2 |
          |-------------------------------------------|
          |    Karim        kunin          hamma    1 |
          |    Karim        kunin          kunin    2 |
          |    Karim        kunin         tafida    3 |
          |-------------------------------------------|
          |   gassol       gassol        bajumba    1 |
          |   gassol       gassol        doubeli    2 |
          |-------------------------------------------|
          |   gassol   mutum biyu          agure    1 |
          |   gassol   mutum biyu   garin magaji    2 |
          |   gassol   mutum biyu         namnai    3 |
          |   gassol   mutum biyu         nanido    4 |
          |-------------------------------------------|
          |   gassol       tutare        gunduma    1 |
          |   gassol       tutare         tutare    2 |
          |-------------------------------------------|
          |  jalingo      jalingo         bashin    1 |
          |  jalingo      jalingo         magami    2 |
          |  jalingo      jalingo      mayo gwoi    3 |
          |  jalingo      jalingo        sintali    4 |
          |  jalingo      jalingo     wuro sambe    5 |
          |-------------------------------------------|
          |  jalingo         kona           garu    1 |
          |  jalingo         kona   jauro nokiya    2 |
          |  jalingo         kona       jekunoho    3 |
          |  jalingo         kona      mayo dasa    4 |
          |-------------------------------------------|
          |    yorro   kpantisawa          bille    1 |
          |    yorro   kpantisawa          kassa    2 |
          |    yorro   kpantisawa     kpantisawa    3 |
          |    yorro   kpantisawa          zokwa    4 |
          |-------------------------------------------|
          |    yorro       pupule        bajumba    1 |
          |    yorro       pupule          kwaji    2 |
          |    yorro       pupule           mika    3 |
          |    yorro       pupule           tula    4 |
          +-------------------------------------------+

        Comment


        • #5
          thanks for your kind response. in particular Roman's post has generated the required ids.

          Comment

          Working...
          X