Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: indirect evaluation of variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: indirect evaluation of variables
Date   Mon, 31 Mar 2003 11:11:19 +0100

Bill Rising

> I've run into a situation where I'd like to build an
> expression which
> contains different variables depending on the observation.
> Now, this
> could be done by nesting a series of -cond()-s, though it
> would be a bit
> complex (and hard to generalize).
>
> Is there some way to use variables a pointers (use indirect
> evaluation of
> the variables)? For example suppose that there were three variables:
>
> bleen bling blam
> ing      1    11
> am       2    12
> ing      3    13
> ing      4    14
>
> I'd like to be able to have
>
> gen blob = eval("bl"+bleen), so that the dataset would then be
>
> bleen bling blam  blob
> ing      1    11     1
> am       2    12    12
> ing      3    13     3
> ing      4    14     4
>
> where the (non-existant) -eval()- function takes its string
> argument and
> interprets it as the name of a variable.
>
> I vaguely recall being stumped by this once in the past,
> and know that
> I'm stumped once again. Any tips would be much appreciated,
> even negative
> ones.

As Bill says, there is a -cond()- solution:

gen blob = cond(bleen == "ing", bling, blam)

Macros could also be used with the downside
of a great loss in speed:

gen blob = .
forval i = 1 / `= _N' {
	replace blob = bl`= `bleen'[`i']' in `i'
}

Nick
[email protected]

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index