Announcement

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

  • using ambiguous variable abbreviations

    Hello,

    I have a dataset with a lot of string variables with a common prefix that I want to use when generating a flag variable. More specifically, I have the variables "xx1a", "xx1b", "xx1c" . . . "xx1h", and I want to write a code like:

    Code:
    replace flagvar = 1 if xx1`*' == "yyyy"
    But that returns the error: "xx1 ambiguous abbreviation". I realize that "xx1" is an ambiguous abbreviation, as there are multiple variables with that same prefix in there (with suffixes "a" through "h"). What is the correct code for making it such that Stata recognizes the command is including the "or" ("|") operator between each of the if statements for each of the variables with the prefix "xx1", so that I'm basically asking Stata to replace flagvar with a value of 1 if the value for any of the variables with prefix "xx1" equals "yyyy", so that I don't have to manually type in:

    Code:
    replace flagvar = 1 if xx1a=="yyyy" | if xx1b=="yyyy" | if xx1c=="yyyy" | if xx1d=="yyyy" //etc etc
    Thanks!

    Robert

  • #2
    try this:
    Code:
    foreach var of varlist xx1* {
       replace flagvar=1 if `var' == "yyyy"
    }

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      try this:
      Code:
      foreach var of varlist xx1* {
      replace flagvar=1 if `var' == "yyyy"
      }
      Yes--great, thanks! I don't know why I was initially hesitant to use a loop command, but that works just fine.

      Comment


      • #4
        See also -anymatch- under -help egen-. My standard general advice is that if you can't think of how to do something that seems in principle and ordinary, look at -egen-.

        Comment

        Working...
        X