Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: RE: using value of one variable to read value of specific other variable out of a list of variables

 From arne.weiss@uni-erfurt.de To statalist@hsphsun2.harvard.edu Subject Re: st: RE: RE: using value of one variable to read value of specific other variable out of a list of variables Date Mon, 09 May 2011 20:09:38 +0200

```Nick Cox schrieb:
```
For a longer list of x variables, you could do this: gen z = . qui forval j = 1/15 { replace z = x`j' if y == `j' } Nick n.j.cox@durham.ac.uk
```
-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 09 May 2011 18:35
To: 'statalist@hsphsun2.harvard.edu'
Subject: st: RE: using value of one variable to read value of specific other variable out of a list of variables

```
gen z = cond(y == 1, x1, cond(y == 2, x2, x3)) Nick n.j.cox@durham.ac.uk
```arne.weiss@uni-erfurt.de

```
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.
```

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```
```Thanks, that's it!
Arne

--
Dr. Arne Robert Weiß
```
University of Erfurt Nordhäuser Str. 63, D-99089 Erfurt Tel: +49 361 737 4522
```Fax: +49 361 737 4569
```
http://www.uni-erfurt.de/mikrooekonomie/lehrstuhlteam/dr-arne-weiss http://arneweiss.blogspot.com
```*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```