Announcement

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

  • How to create drug classification variable using generic drug name variable

    Hello. I am research student that is new to STATA and I am trying to learn how to create a drug classification variable in my dataset. I have a variable with generic drug name, dose, and form (ex. labetalol 200 mg tablet). I would like to write a code that tells STATA to find a list of drug names, for example "labetalol", "atenolol", "carvedilol" and label them as a "beta blocker" in the new variable drug classification.
    I have tried using the find and replace method as detailed in other forums but have been unsuccessful with the codes. Thanks in advance for the help.

  • #2
    Below, you may need to add more punctuation characters that delimit your words. I have included the common ones. Note the lowercase entry of drug names.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str50 drugname
    "MDMA (Ecstasy/Molly)"                              
    "Over-the-Counter Medicines--Dextromethorphan (DXM)"
    "Marijuana (Cannabis)"                              
    "Carvedilol."                                      
    "LSD."                                              
    "Over-the-Counter Medicines--Loperamide."          
    "Kratom."                                          
    "Methamphetamine."                                  
    "labetalol 200 mg tablet."                          
    "Mescaline (Peyote)"                                
    "Prescription-atenolol-50mg."                      
    end
    
    gen classification=""
    replace classification= "beta blocker" if ustrregexm(" " + lower(drugname) + " ", `"['!?,\.\%\$\#\@\!\-\_\(\) " ](labetalol|atenolol|carvedilol)['!?,\.\%\$\#\@\!\-\_\(\) " ]"')
    l, sep(0)
    Res.:

    Code:
    . l, sep(0)
    
         +-------------------------------------------------------------------+
         |                                           drugname   classifica~n |
         |-------------------------------------------------------------------|
      1. |                               MDMA (Ecstasy/Molly)                |
      2. | Over-the-Counter Medicines--Dextromethorphan (DXM)                |
      3. |                               Marijuana (Cannabis)                |
      4. |                                        Carvedilol.   beta blocker |
      5. |                                               LSD.                |
      6. |            Over-the-Counter Medicines--Loperamide.                |
      7. |                                            Kratom.                |
      8. |                                   Methamphetamine.                |
      9. |                           labetalol 200 mg tablet.   beta blocker |
     10. |                                 Mescaline (Peyote)                |
     11. |                        Prescription-atenolol-50mg.   beta blocker |
         +-------------------------------------------------------------------+

    Comment

    Working...
    X