Announcement

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

  • Listing consecutive numbers in a dash operator

    Dear all,

    Suppose I have the following data
    Code:
    input str8 groupid str8 rangeid
    "AA" "122-5"
    "BB" "140-6"
    "CC" "153-6"
    "DD" "171-9"
    end
    I'd like to list consecutive numbers in dash operators (i.e 122-5 into 122,123,124,125) and add them to separate rows instead of columns.

    Could anyone please help with this?

    Many thanks,

    Vinh

  • #2
    This works for your example. It would break down for 37-42 or 111-42 and many other variants that people would find easy to decode.

    Code:
    clear
    input str8 groupid str8 rangeid
    "AA" "122-5"
    "BB" "140-6"
    "CC" "153-6"
    "DD" "171-9"
    end
    
    gen start = real(substr(rangeid, 1, 3))
    gen end = real(substr(rangeid, 1, 2) + substr(rangeid, -1, 1))
    gen expand = end - start + 1
    expand expand
    bysort groupid : gen newid = start + _n - 1
    drop start end expand
    list, sepby(groupid)
    
         +---------------------------+
         | groupid   rangeid   newid |
         |---------------------------|
      1. |      AA     122-5     122 |
      2. |      AA     122-5     123 |
      3. |      AA     122-5     124 |
      4. |      AA     122-5     125 |
         |---------------------------|
      5. |      BB     140-6     140 |
      6. |      BB     140-6     141 |
      7. |      BB     140-6     142 |
      8. |      BB     140-6     143 |
      9. |      BB     140-6     144 |
     10. |      BB     140-6     145 |
     11. |      BB     140-6     146 |
         |---------------------------|
     12. |      CC     153-6     153 |
     13. |      CC     153-6     154 |
     14. |      CC     153-6     155 |
     15. |      CC     153-6     156 |
         |---------------------------|
     16. |      DD     171-9     171 |
     17. |      DD     171-9     172 |
     18. |      DD     171-9     173 |
     19. |      DD     171-9     174 |
     20. |      DD     171-9     175 |
     21. |      DD     171-9     176 |
     22. |      DD     171-9     177 |
     23. |      DD     171-9     178 |
     24. |      DD     171-9     179 |
         +---------------------------+
    In Stata we say observations, not rows, and variables, not columns.

    Comment


    • #3
      Romalpa Akzo It's Nick Cox.

      Comment


      • #4
        Please kindly accept my terrible sorry. I have deleted my post with such mistake.

        Comment


        • #5
          Dear all,

          Thank you very much for your help and suggestions.

          Cheers,

          Vinh

          Comment


          • #6
            Romalpa Akzo : No problem my side.

            Comment

            Working...
            X