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

st: RE: assignment by indexing


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: assignment by indexing
Date   Tue, 3 Feb 2004 12:46:59 -0000

No special way of doing this springs to mind. 

gen newvar = x1 if index == 1
forval i = 2/5 { 
	replace newvar = x`i' if index == `i' 
} 

or for those who were LISP programmers in 
some previous phase 

gen newvar = cond(index==1, x1, 
             cond(index==2, x2, 
		 cond(index==3, x3, 
		 cond(index==4, x4, 
             cond(index==5, x5, .)))))

Nick 
[email protected] 

Ben Pelzer
 
> Since I'm a stata-rooky this question probably is very basic. 
> I'm looking
> for a way to copy the contents of a specific variable out of 
> the list "x1 x2
> x3 x4 x5" to a new variable, newvar, say. However, which 
> variable to copy
> depends on the value of another variable, index, say. So I would need
> something like:
> 
> generate newvar = x(index).
> 
> How can I realize this in Stata? I looked through the P 
> programming and the
> U user manual, but got stuck. 

*
*   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