Announcement

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

  • Splitting a string variable along "/"

    Hi. In the following data, I want to do two things:

    1. split the Age variable into two variables - age and sex using the "/" as the condition in the Age variable
    2. In the remaining age variable for all ages less than a year, replace it with "1". For example, in observation 2 and 3 below, the age variable would be "1" for both

    Any help would be much appreciated.


    Code:
    input str15 MRNo str8 Age
    "P1610634"     "68/F"    
    "P1602163"     "1 Mo. /M"
    "P1602154"     "7 Mo. /M"
    "PN1585614"    "1/F"     
    "PN1401224"    "2/M"     
    "P1609543"     "63/M"    
    "P1610326"     "46/F"    
    "P1257195"     "4/F"     
    "P1411821"     "2/M"     
    "P1539542"     "30/M"    
    "P1610610"     "69/F"    
    "N500364"      "50/M"    
    "KVC-P145026"  "57/F"    
    "P352115"      "33/F"    
    "P1541985"     "26/F"    
    "P1519669"     "21/F"    
    "P1598091"     "73/M"    
    "N492028"      "9/M"

  • #2
    Code:
    split Age, gen(age) parse("/")
    replace age1 = "1" if ustrregexm(age1, "[0-9]{1,2} Mo.")
    destring age1, replace
    rename age1 age
    rename age2 sex

    Comment


    • #3
      I suggest that ages less than 1 year be recoded 0, not 1. If you don't agree, how to change this code should be clear.

      Code:
      clear 
      input str15 MRNo str8 Age
      "P1610634"     "68/F"    
      "P1602163"     "1 Mo. /M"
      "P1602154"     "7 Mo. /M"
      "PN1585614"    "1/F"     
      "PN1401224"    "2/M"     
      "P1609543"     "63/M"    
      "P1610326"     "46/F"    
      "P1257195"     "4/F"     
      "P1411821"     "2/M"     
      "P1539542"     "30/M"    
      "P1610610"     "69/F"    
      "N500364"      "50/M"    
      "KVC-P145026"  "57/F"    
      "P352115"      "33/F"    
      "P1541985"     "26/F"    
      "P1519669"     "21/F"    
      "P1598091"     "73/M"    
      "N492028"      "9/M"
      end
      
      split Age, parse(/)
      gen age = cond(real(Age1) < ., real(Age1), 0) 
      
      tab Age1 if age == 0 
       
      rename Age2 sex 
      
      list Age* age sex  
      
           +-------------------------------+
           |      Age     Age1   age   sex |
           |-------------------------------|
        1. |     68/F       68    68     F |
        2. | 1 Mo. /M   1 Mo.      0     M |
        3. | 7 Mo. /M   7 Mo.      0     M |
        4. |      1/F        1     1     F |
        5. |      2/M        2     2     M |
           |-------------------------------|
        6. |     63/M       63    63     M |
        7. |     46/F       46    46     F |
        8. |      4/F        4     4     F |
        9. |      2/M        2     2     M |
       10. |     30/M       30    30     M |
           |-------------------------------|
       11. |     69/F       69    69     F |
       12. |     50/M       50    50     M |
       13. |     57/F       57    57     F |
       14. |     33/F       33    33     F |
       15. |     26/F       26    26     F |
           |-------------------------------|
       16. |     21/F       21    21     F |
       17. |     73/M       73    73     M |
       18. |      9/M        9     9     M |
           +-------------------------------+

      Comment

      Working...
      X