Announcement

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

  • Loops and subinstr


    Hello, I would like to replace 10 variables with bits of characters.
    Code:
    foreach i of numlist 1/10 {
    clonevar chimiomol`i'=hc_chimiomol`i'
    replace chimiomol`i'=subinstr(chimiomol`i',"Imm1","Alemtuz umab",.)
    replace chimiomol`i'=subinstr(chimiomol`i',"Imm2","Gemtuzu mab",.)
    replace chimiomol`i'=subinstr(chimiomol`i',"Imm3","Ibritum omab",.)
    replace chimiomol`i'=subinstr(chimiomol`i',"Imm4","Rituxim ab",.)
    }

    Command:
    (17 real changes made)
    (16 real changes made)
    (0 real changes made)
    (1,400 real changes made)

    The problem: When I run the code, there is only the last line is replaced (Imm4 -> Rituximab). In stata command, it tells me that all the patterns have been replaced but this is not the case.


    An idea of ​​the problem and how to fix it ?
    Thank you.


  • #2
    Example data please. We need to see a few variables with examples of each of lmm1 lmm2 lmm3 lmm4 somewhere.

    Comment


    • #3
      chimiomol1 chimiomol2
      Rituximab+31+24 Rituximab+31+24+19+39+16+25+15+Imm1
      Rituximab+31+24 Rituximab+31+24+19+39+16+25+15+Imm2
      39+Rituximab+31+Imm3+16

      Comment


      • #4
        Your code works for me on your example data.
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str23 hc_chimiomol1 str35 hc_chimiomol2
        "Rituximab+31+24"         "Rituximab+31+24+19+39+16+25+15+Imm1"
        "Rituximab+31+24"         "Rituximab+31+24+19+39+16+25+15+Imm2"
        "39+Rituximab+31+Imm3+16" ""                                   
        end
        foreach i of numlist 1/2 {
            clonevar chimiomol`i'=hc_chimiomol`i'
            replace chimiomol`i'=subinstr(chimiomol`i',"Imm1","Alemtuz umab",.)
            replace chimiomol`i'=subinstr(chimiomol`i',"Imm2","Gemtuzu mab",.)
            replace chimiomol`i'=subinstr(chimiomol`i',"Imm3","Ibritum omab",.)
            replace chimiomol`i'=subinstr(chimiomol`i',"Imm4","Rituxim ab",.)
        }
        list *1, clean abbreviate(16)
        list *2, clean abbreviate(16)
        Code:
        . list *1, clean abbreviate(16)
        
                         hc_chimiomol1                        chimiomol1  
          1.           Rituximab+31+24                   Rituximab+31+24  
          2.           Rituximab+31+24                   Rituximab+31+24  
          3.   39+Rituximab+31+Imm3+16   39+Rituximab+31+Ibritum omab+16  
        
        . list *2, clean abbreviate(16)
        
                                     hc_chimiomol2                                    chimiomol2  
          1.   Rituximab+31+24+19+39+16+25+15+Imm1   Rituximab+31+24+19+39+16+25+15+Alemtuz umab  
          2.   Rituximab+31+24+19+39+16+25+15+Imm2    Rituximab+31+24+19+39+16+25+15+Gemtuzu mab  
          3.
        I have to wonder why your code loops over 10 variables but your sample output shows only one set of four replace results. Are you sure the variable you were looking at corresponds to the variable that produced the 4 results?

        Comment


        • #5
          thank you for you answer.
          I performed the same manipulation and I confirm that only Imm4 is replaced. I really don't understand why it works for you and not for me...

          Code:
          clear
          input  str40 hc_chimiomol1  
          "Imm4+31+24+19+39+16+25+15+Imm1"
          "Imm4+31+24+19+39+16+25+15+Imm2+Imm3"
          "39+Imm4+31+Imm3+16"      
          end                           
           
          
          foreach i of numlist 1/1 {
          clonevar chimiomol`i'=hc_chimiomol`i'
          replace chimiomol`i'=subinstr(hc_chimiomol`i',"Imm1","Alemtuzumab",.)
          replace chimiomol`i'=subinstr(hc_chimiomol`i',"Imm2","Gemtuzumab",.)
          replace chimiomol`i'=subinstr(hc_chimiomol`i',"Imm3","Ibritumomab",.)
          replace chimiomol`i'=subinstr(hc_chimiomol`i',"Imm4","Rituximab",.)
          }
          Code:
          input str40 hc_chimiomol1 str42 chimiomol1
          "Imm4+31+24+19+39+16+25+15+Imm1"      "Rituximab+31+24+19+39+16+25+15+Imm1"    
          "Imm4+31+24+19+39+16+25+15+Imm2+Imm3" "Rituximab+31+24+19+39+16+25+15+Imm2+Imm3"
          "39+Imm4+31+Imm3+16"                  "39+Rituximab+31+Imm3+16"


          Anyone have an idea of ​​the problem?

          Comment


          • #6
            First off, in some posts here your strings look like lmm1 (start with lower case l) and in others they look like Imm1 (upper case I). That's why data should be given as code in a font that is not ambiguous.

            That's not the most recent problem however. You create a new variable but the only change that bites is the last one. @William Lisowski's code differs from yours because you need to keep substituting in your new variable, not in the previously existing one.

            So, I think your code looked right in #1 except possibly for confusing those two letters, whereas your code is wrong in #5 for the reason just given.

            Comment

            Working...
            X