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

RE: st: assignment by indexing


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: assignment by indexing
Date   Tue, 3 Feb 2004 13:23:40 -0000

Allan is incorrect in saying that 

gen y = x[rankx] 

is not allowed in Stata. That is perfectly 
legal, and sometimes very useful. In general, 
expressions are allowed in subscripts, and evaluated
observation by observation. Otherwise [_n-1] 
would not do what it does. In general, it is 
naturally important to be clear that the expression 
evaluates to an appropriate integer. (Stata uses 
floor() to round non-integers.) 

However, this doesn't help in the problem 
Ben posted, as he has several variables, not one. 

Turning to Allan's own problem, one way round 
his difficulty over _N is 

forval i = 1/`=_N' { 

However, 

in rankx 

is illegal. 

gen newvar = (index == 1) * x1 + (index == 2) * x2 ... 

offers another approach to Ben's problem. Note
that if -index- is never one of the specified values
then -newvar- is 0. 

Nick 
n.j.cox@durham.ac.uk 

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Allan Reese
> Sent: 03 February 2004 12:55
> To: Stata distribution list
> Subject: Re: st: assignment by indexing 
> 
> 
> On Tue, 3 Feb 2004, Ben Pelzer wrote:
> > 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).
> 
> Unless I have misunderstood the question, Ben has fingered 
> something that
> is a curious gap in Stata, but I may be biased here since 
> another package
> provides exactly what he describes.  To clarify the 
> terminology, he wants
> to assign values from one variable into another, with the 
> permutation of
> values determined by a third variable.  So
> 
> *   gen  y = x[rankx]
> 
> where "rankx" holds the rank of the corresponding value in x, 
> would assign
> the x values in order into y.
> 
> Unfortunately, * is not currently supported by Stata, and 
> appears to be
> one situation where an explicit loop is required.  I used 
> such code in an
> ado file, but offer here a simplified untested retyping.  One 
> oddity was
> that _N could not be written in the for command
> 
>    loc nn = _N
>    gen y = .
>    forvalues i = 1/`nn' {
>       replace y = x in rankx
>    }

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index