Announcement

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

  • Splitting a string variable in to three parts


    Dear All,
    I have a string variable and I want to divide it into three parts. Here's the example.


    Code:
    clear
    input str50 nic_code
    "Division 43 Specialized construction activities"
    "Division 49 Land transport and transport via pipelines"
    "Division 69 Legal and accounting activities"
    "Group 421 Construction of roads and railways"
    "Group 451 Sale of motor vehicles"
    "Group 461 Wholesale on a fee or contract basis"
    "Group 432 Electrical, plumbing and other construction installation activities"
    "Section A Agriculture, forestry and fishing"
    "Section B Mining and quarrying"
    "Section C Manufacturing"
    end
    I want to divide the above strings to three parts such as:

    Code:
    clear
    input str10 x1 str4 x2 str50 x3
    "Division" "43" "Specialized construction activities"
    "Division" "69" "Legal and accounting activities"
    "Group" "421" "Construction of roads and railways"
    "Section" "A" "Agriculture, forestry and fishing"
    end
    and so on.

    Any help will be greatly appriciated.

  • #2
    Code:
    gen wanted1 = word(nic_code, 1) 
    gen wanted2 = word(nic_code, 2) 
    gen work = subinstr(nic_code, wanted1, "", 1)
    replace work = subinstr(work, wanted2, "", 1) 
    gen wanted3 = trim(work) 
    
    list wanted?

    Comment


    • #3
      Nick Cox Thank you, Sir. That is exactly what I wanted. I was also unfamiliar with the word function.

      Comment


      • #4
        Just for the sake of playing with Stata (in this "social isolation" week end), I tested this code:

        Code:
        split nic_code
        gen nic_code3b = nic_code3 + " " + nic_code4 + " " + nic_code5 + " " + nic_code6 + " " + nic_code7 + " " + nic_code8 + " " + nic_code9
        list nic_code1 nic_code2 nic_code3b
         +------------------------------------------------------------------+
             | nic_co~1   nic_co~2                                   nic_code3b |
             |------------------------------------------------------------------|
          1. | Division         43      Specialized construction activities     |
          2. | Division         49      Land transport and transport via pipel  |
          3. | Division         69           Legal and accounting activities    |
          4. |    Group        421         Construction of roads and railways   |
          5. |    Group        451                    Sale of motor vehicles    |
             |------------------------------------------------------------------|
          6. |    Group        461         Wholesale on a fee or contract basis |
          7. |    Group        432   Electrical, plumbing and other construct   |
          8. |  Section          A         Agriculture, forestry and fishing    |
          9. |  Section          B                     Mining and quarrying     |
         10. |  Section          C                          Manufacturing       |
             +------------------------------------------------------------------+
        Best regards,

        Marcos

        Comment


        • #5
          Marcos Almeida Thank you for that cool code!

          Comment

          Working...
          X