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

st: RE: referencing variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: referencing variables
Date   Thu, 9 Jan 2003 18:16:20 -0000

[email protected]
> 
> I was wondering if anyone can help me on this programming 
> problem.  I have
> an array of variables, let's say x1-x20, and I wish to 
> refer to one of them
> using values in another variable (let's call it m), in an 
> equation so that
> I can use one equation that is different for each 
> observation.  In other
> words, if my equation is y = x`m' + 1 and m = 2, then the 
> equation for that
> particular observation would be y = x2 + 1.  Here is a 
> partial list of what
> I'm talking about:
> 
> obs    m      equation
> 
> 1          2       y = x2 + 1
> 2          3       y = x3 + 1
> 3          6       y = x6 + 1
> 4          8       y = x8 + 1
> 
> I don't think using a global or macro variable works 
> because those only
> take on one value and because the equation is essentially 
> the model in a
> non-linear estimation, I don't think I could use a loop.  I 
> was wondering
> if there are more options out there.  Thank you!

Something like this may be what you want.
 
qui gen y = . 
local n = _N 
qui forval i = 1 / `n' { 
	local m = m[`i'] 
	replace y = x`m' + 1 in `i' 
} 

In Stata 8, this can be shortened a little: 

qui gen y = . 
qui forval i = 1 / `=_N' { 
	local m = m[`i'] 
	replace y = x`m' + 1 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