Hi,

I have a list of variables that look as follows: max_1 max_2 max_3. All the numbers in the variables correspond to a certain colour e.g. 1==blue, 2==yellow, 3==orange.

What I want to do is:

gen blue=1 if max_1!=.
gen yellow=1 if max_2!=.
gen orange=1 if max_3!=.

and so on.


I have tried to do this with a loop as follows:

local colours "blue yellow orange"

foreach colour of local colours{
forvalues i = 1(1)3{
preserve
gen `colour'_present = 1 if max_`i'!=.
restore
}
}

however, this does not work because it keeps on looping. Is there a way around this/ or to achieve the same result?

Thanks