# Re: st: Looking up which observation has a particular value

 From "Sergiy Radyakin" <[email protected]> To [email protected] Subject Re: st: Looking up which observation has a particular value Date Thu, 7 Aug 2008 20:09:39 -0400

```this is rather destructive way to do this but if you need to do this only once:

keep if name=="Jones"
local Jones_age=age[1]

Another way is:
gen obsn=_n
sum obsn if name=="Jones"
local Jones_age=age[`=r(min']

and there are lots of other ways to do the same.

If you want to be fast - write a loop in Mata or C - then you don't
need to create another variable.

Depending on the task, you might want to sort your data and then go
by(name) to do something.

If this doesn't help, tell us more what you are doing. VLOOKUP is
probably not here for a good reason.

Also see if -merge- can be useful. (eg. if you have a dataset with
names and another larger one with names and ages - you don't need to
look for each name individually, just merge the two datasets by name).

Regards,

On 8/7/08, Brian Karfunkel <[email protected]> wrote:
> Hello everyone,
>
> If I have a dataset that looks like this:
>
>     +--------------------+
>     |    name   age |
>     |---------------------|
>  1. |   Smith    42 |
>  2. |  Kellog    28 |
>  3. | McCairn    19 |
>  4. |   Jones    22 |
>     +-------------------+
>
> I want to use a particular value of one variable to look up, and then
> store in a local (or scalar, if need be) the value of another variable
> iIN THAT SAME ROW. In other words, I want to do something like this
> (this is of course pseudocode; I understand that locals don't work
> like this):
>
> - local jones_age = age if name == "Jones"
>
> or maybe:
>
> - local jones_obs = (some function that returns the row number of the
> first observation for which name == "Jones")
> - local jones_age = age in `jones_obs'
>
> One thought I had was to either create a variable, say age_jones, and:
> - gen age_jones = age if name == "Jones"
>
> And then maybe -sum- age_jones, using the return code to find the age
> (which if the obs. is unique should be min, max, and mean), but since
> my data is actually many more than four observations long, this seems
> rather cumbersome. For those of you familiar with Excel, I'm looking
> for something similar to the VLOOKUP and HLOOKUP commands.
>
> Any suggestions? Is there a way to create locals from variable values?
>
>
> -Brian Karfunkel
> *
> *   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/
```