Announcement

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

  • Using foreach loop for string variable

    Hello, I am new to looping and thought I could get some guidance here. I typed

    Code:
    foreach var of varlist uuid1 uuid2 uuid3 uuui4 {
        replace `var' = "ABC-ALB-ICE-011"/"ABC-ALB-ICE-017" if `var'=="ABC-ALB-ICE-0011"/"ABC-ALB-ICE-0017"
    }
    and I got this response
    Code:
    type mismatch
    What am I missing? Thank you.

    Rogers

  • #2
    Originally posted by Rogers Ssebunya View Post
    What am I missing?
    What is it that you intend with the expression "ABC-ALB-ICE-011"/"ABC-ALB-ICE-017"? The solidus is an arithmetic operation, which cannot be performed on strings.

    Comment


    • #3
      Code:
      foreach var of varlist uuid1 uuid2 uuid3 uuui4 {
          replace `var' = "ABC-ALB-ICE-011" if `var'=="ABC-ALB-ICE-0011"
          replace `var' = "ABC-ALB-ICE-017" if `var'=="ABC-ALB-ICE-0017"
      }
      or
      Code:
      foreach var of varlist uuid1 uuid2 uuid3 uuui4 {
          replace `var' = subinstr(`var',"0","",1)
      }

      Comment


      • #4
        I am with Chen Samulsion and confirm that your problem is with the string expression fed to replace. Note that even if you intended | (logical OR) that operator requires numeric operands.

        Being in a loop is not itself a problem with your code.

        The function inlist() is worth knowing here, even if it is not needed.

        Comment


        • #5
          Thank you Chen for options they have been helpful & Nick thank you for the "inlist" command, am reading about it.

          Comment

          Working...
          X