Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Tue, 10 May 2011 08:48:11 +0100 |

Christian's answer will work fine. But note that foreach n of numlist 1/3 { is always better rewritten as forval n = 1/3 { In essence, why does have Stata have a -forval- command when you can always use -foreach- with a -numlist-? -forval- is concise and efficient about going through its numlist. In an example like this, the efficiency gain will be minute, but the general point remains. Nick On Mon, May 9, 2011 at 7:14 PM, Christian Holz (Stata list) <statalist@holz.ca> wrote: > what about this? > > generate z=. > replace z=x1 if y==1 > replace z=x2 if y==2 > replace z=x3 if y==3 > > or more generically (ie if there are more than 3 possibilities) > > generate z=. > foreach n of numlist 1/3 { > replace z=x`n' if y==`n' > } arne.weiss@uni-erfurt.de> wrote: >> I am sure there is some obvious solution that I am not seeing, but I >> couldn't figure it out: >> 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/

**References**:**st: using value of one variable to read value of specific other variable out of a list of variables***From:*arne.weiss@uni-erfurt.de

**Re: st: using value of one variable to read value of specific other variable out of a list of variables***From:*"Christian Holz (Stata list)" <statalist@holz.ca>

- Prev by Date:
**Re: st: manipulate ordering of rows matrices** - Next by Date:
**Re: st: manipulate ordering of rows matrices** - Previous by thread:
**Re: st: using value of one variable to read value of specific other variable out of a list of variables** - Next by thread:
**st: esttab equivalent for svy data** - Index(es):