Tidy way to generate a new variable based on information from many other variables?

#1
Hi All,

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.
 
#2
Re: Tidy way to generate a new variable based on information from many other variable

You could do something like:
foreach x of varlist x1-x8 {
gen `x'new = 1
}