First point is to analyse the logic. Reforming a problem often speeds up
the execution far more than fancy coding, and leads to insights. In this
case:
gen y=5
replace y=3 if x1==1 & x2==3
replace y=4 if y==5 & (x3==2 & x4==17)
Actually, the y==5 part would be a nice way of simplifying the coding and
avoiding errors in this case. However, suppose instead that you couldn't
count on Y equaling 5, e.g. suppose the first statement was more like gen
y=x12, and you couldn't count on x12 not equaling a value that would also
be assigned via a replace statement. Then, you'd have to use a more
complicated syntax like I had before.