Announcement

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

  • Separate string variable

    I want to separate a string variable and create a new variable if the word "Chest" or "chest" appears in the original string variable.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 A str42 B
    "id " "diagnosis"                                 
    "1"   "Acute carpal tunnel syndrome of left wrist"
    "2"   "Acute cerebral infarction"                 
    "3"   "Acute cerebral infarction"                 
    "4"   "Acute cervical myofascial strain"          
    "5"   "Acute cervical myofascial strain"          
    "6"   "Acute cervical myofascial strain"          
    "7"   "Acute cervical myofascial strain"          
    "8"   "Acute cervical myofascial strain"          
    "9"   "Acute cervical myofascial strain"          
    "10"  "Acute chest wall pain"                     
    "11"  "Acute chest wall pain"                     
    "12"  "Acute chest wall pain"                     
    "13"  "Acute chest wall pain"                     
    "14"  "Acute cholecystitis"                       
    "15"  "Acute cholecystitis"                       
    "16"  "Acute cholecystitis"                       
    "17"  "Chest pain"                                
    "18"  "Chest pain"                                
    "19"  "Chest pain"                                
    "20"  "Chest pain"                                
    "21"  "Chest pain"                                
    "22"  "Chest pain"                                
    "23"  "Chest pain"                                
    "24"  "Chest pain"                                
    "25"  "Chest pain"                                
    "26"  "Chest pain"                                
    "27"  "Chest pain"                                
    "28"  "Chest pain"                                
    end


  • #2

    Code:
    gen chest_mention = strpos(B, "chest") | strpos(B, "Chest")
    as if either string position is positive (non-zero), the result is true or 1. Another way to do it s

    Code:
    gen chest_mention = strpos(lower(B), "chest") > 0 
    Last edited by Nick Cox; 20 Jul 2023, 00:36.

    Comment


    • #3
      Thanks for the solution, Nick!

      Comment

      Working...
      X