Announcement

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

  • Finding a match rowwise in several variables






    This question has appeared under different names as

    http://www.talkstats.com/showthread....ther-variables

    https://www.reddit.com/r/stata/comme...able_based_on/

    with no replies to date. I know why I am not active in either place -- although manifestly I glance at both sites. Perhaps the person concerned knows why they will ask anywhere but Statalist!

    Suppose I have a set of variables {x1, x2, x3, x4, x5, x6, x7, x8...}. I would like to generate a new variable that takes a value of 1 if x1=1 or if x2 =1 or if x3=1 etc...

    I have been doing this crudely by:

    gen newvar = 0
    replace newvar = 1 if x1==1 | x2==1 | x2==1 etc...

    I presume there is a tidier way of doing this? Any help is much appreciated.
    Answer: There are at least three ways to do this:

    Code:
    * #1
    gen newvar = 0
    
    quietly forval j = 1/8 {
        replace newvar = 1 if x`j' == 1
    }
    
    * #2
    
    gen newvar = inlist(1, x1, x2, x3, x4, x5, x6, x7, x8)
    
    * #3
    
    egen newvar = anymatch(x1-x8), values(1)
Working...
X