I want to create a variable (z) that contains the value from one
variable out of a list of variables (say x1 - x3) depending on the
valuable of another variable (y). The list of variables is a) named in a
way that the value of y could be used to refer to the correct name of
the variable and b) ordered so that the value of y could also be used a
locational reference (as the offset command does in Excel). I don't
think b) could work in STATA, but I couldn't find a solution for a)
either. See below an example. Therein, z in the first row is supposed to
be 1 and in the second row 5 (if y = 2, z is supposed to have the value
of x2).

x1 x2 x3 y
1 2 3 1
0 5 4 2

Here is the closest I came towards a solution:

local a = y
gen z = x`a'

This command, however, only does half the job as the value of the local
a is not being rest for every row. I would be very happy for any advice.

Thanks, Nick. That is already much better than repeated if qualifiers.
However, my actual set of variables is longer than 3 variables, and I
have the same type of problem several times in my data-set. Before doing
it "brute force", would you happen to know a more "elegant" solution?
Thanks already anyway!