Announcement

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

  • Conditioning on a string variable

    Hi, I'm new to Stata and would like to know how to condition on a string variable. I have a variable 'drugs' with names of different drugs e.g., paracetamol, penicillin etc. I want to create new variables as 'Paracetamol' or 'Antibiotics' where I want to flag as '1' the respective drug e.g.
    gen paracetamol = 1 if drugs=="paracetamol" | "paracet" | "panadol" | "padol"
    gen antibiotics = 1 if drugs=="penicillin" | "amoxicillin" | "antibio"



    While I'm trying to do it, I get the error 109 (type mismatch). I get the same error even after encoding the 'drugs' variable as a new variable "drugs_new".
    Kindly help!
    Last edited by Wali Amar; 01 Oct 2021, 13:46.

  • #2
    This question cannot be answered without showing example data. Your description of the data in the two paragraphs is implicitly contradictory. On the one hand you seem to think that drugs is a string variable, and on the other you speak of an encoded variable, which would be numeric. Nobody can tell what you have, and without that, noone can tell you what to do.

    Please fire up the -dataex- command and post an example. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Hi, thanks for your reply. Here's how the drugs variable looks like:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str218 drugs
      "."                                                                                                            
      "."                                                                                                            
      "75 mg paracet"                                                                              
      "Adalat"                                                                                                      
      "Adelat"                                                                        
      "Aduvanz"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius, paracet"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius"                                                                                                      
      "Aerius, paracet"                                                                                                                                                                                                            
      "Aerius
      Somac"                                                                                              
      "Aerius"                                                                                      
      "Aerius"                                                                                                                                          
      "Aerius, lactulose, Zofran, symbicort"                                                                        
      "Aerius, Paracet"                                                                                              
      "Aerius, Paracet, gaviscon"                                                                                                                                                                        
      "Afipran
      Paracet
      Spersadex
      Ibux"                                                                            
      "Afipran
      Naproxen"                                                                            
      "Alby-E"                                                                                                      
      "Albyl E"                                                                                                      
      "Albyl e"                                                                                                      
      "Albyl E"                                                                                                      
      "Albyl-E"                                                                                                      
      "Albyl-E, erius"                                                                                                                                                                                                                                                                                                          
      "Alvesco"                                                                                                                                                                          
      "Amoxicillin"                                                                                                  
      "Amoxicillin mylan"                                                                                                                                                                              
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotika"                                                                                                  
      "Antibiotis"                                                                                  
      Imigran
      Paracet"                                                        
      "Antibiotika"                                  
      "Antibiotika pencelin"                                                      
      "Antibiotics
      Paracet
      Samarin"                                                                                  
      "Antibiotics, lecitin"                                                                                  
      "Antibiotics, paracet"
      "Antibiotics, Penicilin, Paracet"                                                                                            
      end
      Last edited by Wali Amar; 04 Oct 2021, 01:20.

      Comment


      • #4
        This is a very common question. Look at the documentation and search the forum for mentions of the string functions -inlist()-, -regexm()- and -strpos()-. For example, if you want to spell out the names in full, you can start with

        Code:
        gen paracetamol1 = inlist(lower(drug), "paracetamol", "paracet", "panadol", "padol")
        This is limited to 10 strings. Beyond 10, you need to use replace, specifying the condition -if not equal to 1-

        Code:
        replace paracetamol1= 1 if inlist(lower(drug), "tylenol", "excedrin", "calpol") & !paracetamol
        To capture all drugs with the prefix "para", one way is

        Code:
        gen paracetamol2 = regexm(lower(drug), "^para")
        To capture the substring "para" anywhere within the string

        Code:
        gen paracetamol3 = regexm(lower(drug), "para")
        or

        Code:
        gen paracetamol4 = strpos(lower(drug), "para")>0

        You may combine several functions and use logical operators such as or, e.g.,

        Code:
        gen paracetamol5 = regexm(lower(drug), "^para") | inlist(drug, "tylenol", "excedrin", "calpol", "panadol", "padol")
        See

        Code:
        help operator
        Res.:


        Code:
        
        . l, sep(0)
        
             +---------------------------------------------------------------------------------------------+
             |                                drugs   parace~1   parace~2   parace~3   parace~4   parace~5 |
             |---------------------------------------------------------------------------------------------|
          1. |                                    .          0          0          0          0          0 |
          2. |                                    .          0          0          0          0          0 |
          3. |                        75 mg paracet          0          0          1          1          0 |
          4. |                               Adalat          0          0          0          0          0 |
          5. |                               Adelat          0          0          0          0          0 |
          6. |                              Aduvanz          0          0          0          0          0 |
          7. |                               Aerius          0          0          0          0          0 |
          8. |                               Aerius          0          0          0          0          0 |
          9. |                               Aerius          0          0          0          0          0 |
         10. |                               Aerius          0          0          0          0          0 |
         11. |                               Aerius          0          0          0          0          0 |
         12. |                               Aerius          0          0          0          0          0 |
         13. |                               Aerius          0          0          0          0          0 |
         14. |                               Aerius          0          0          0          0          0 |
         15. |                               Aerius          0          0          0          0          0 |
         16. |                               Aerius          0          0          0          0          0 |
         17. |                               Aerius          0          0          0          0          0 |
         18. |                               Aerius          0          0          0          0          0 |
         19. |                               Aerius          0          0          0          0          0 |
         20. |                      Aerius, paracet          0          0          1          1          0 |
         21. |                               Aerius          0          0          0          0          0 |
         22. |                               Aerius          0          0          0          0          0 |
         23. |                               Aerius          0          0          0          0          0 |
         24. |                               Aerius          0          0          0          0          0 |
         25. |                      Aerius, paracet          0          0          1          1          0 |
         26. |                               Aerius          0          0          0          0          0 |
         27. |                                Somac          0          0          0          0          0 |
         28. |                               Aerius          0          0          0          0          0 |
         29. |                               Aerius          0          0          0          0          0 |
         30. | Aerius, lactulose, Zofran, symbicort          0          0          0          0          0 |
         31. |                      Aerius, Paracet          0          0          1          1          0 |
         32. |            Aerius, Paracet, gaviscon          0          0          1          1          0 |
         33. |                              Afipran          0          0          0          0          0 |
         34. |                              Paracet          1          1          1          1          1 |
         35. |                            Spersadex          0          0          0          0          0 |
         36. |                                 Ibux          0          0          0          0          0 |
         37. |                              Afipran          0          0          0          0          0 |
         38. |                             Naproxen          0          0          0          0          0 |
         39. |                               Alby-E          0          0          0          0          0 |
         40. |                              Albyl E          0          0          0          0          0 |
         41. |                              Albyl e          0          0          0          0          0 |
         42. |                              Albyl E          0          0          0          0          0 |
         43. |                              Albyl-E          0          0          0          0          0 |
         44. |                       Albyl-E, erius          0          0          0          0          0 |
         45. |                              Alvesco          0          0          0          0          0 |
         46. |                          Amoxicillin          0          0          0          0          0 |
         47. |                    Amoxicillin mylan          0          0          0          0          0 |
         48. |                          Antibiotika          0          0          0          0          0 |
         49. |                          Antibiotika          0          0          0          0          0 |
         50. |                          Antibiotika          0          0          0          0          0 |
         51. |                          Antibiotika          0          0          0          0          0 |
         52. |                          Antibiotika          0          0          0          0          0 |
         53. |                          Antibiotika          0          0          0          0          0 |
         54. |                          Antibiotika          0          0          0          0          0 |
         55. |                          Antibiotika          0          0          0          0          0 |
         56. |                          Antibiotika          0          0          0          0          0 |
         57. |                          Antibiotika          0          0          0          0          0 |
         58. |                          Antibiotika          0          0          0          0          0 |
         59. |                          Antibiotika          0          0          0          0          0 |
         60. |                           Antibiotis          0          0          0          0          0 |
         61. |                              Imigran          0          0          0          0          0 |
         62. |                              Paracet          1          1          1          1          1 |
         63. |                          Antibiotika          0          0          0          0          0 |
         64. |                 Antibiotika pencelin          0          0          0          0          0 |
         65. |                          Antibiotics          0          0          0          0          0 |
         66. |                              Paracet          1          1          1          1          1 |
         67. |                              Samarin          0          0          0          0          0 |
         68. |                 Antibiotics, lecitin          0          0          0          0          0 |
         69. |                 Antibiotics, paracet          0          0          1          1          0 |
         70. |      Antibiotics, Penicilin, Paracet          0          0          1          1          0 |
             +---------------------------------------------------------------------------------------------+
        
        .
        Last edited by Andrew Musau; 04 Oct 2021, 09:08.

        Comment

        Working...
        X