st: RE: counting per index

 From "Nick Cox" To Subject st: RE: counting per index Date Mon, 15 Nov 2004 21:17:14 -0000

```If I understand this correctly what you want is

gen count = 0
levels limit, local(levels)
qui foreach l of local levels {
count if x < `l'
replace count = r(N) if limit == `l'
}

I think you're right to be puzzled; this seems
a rather unusual problem. The only short-cut
I can think of is looping over levels of -limit-,
rather than over observations.

No solution with -sum()- seems likely to get
anywhere near an answer, as no -sort- order
will segregate observations into relevant
and irelevant.

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

Jelmer Yeb Ypma

> I guess there is a easy solution for this, but after looking
> at it for a couple of hours I and searching the internet for
> the solution I still haven't found it. Now my mind is
> completely lost somewhere, so I hope any of you can help me out.
>
> I have a variable -limit- with limits. e.g.
> 2
> 1
> 0
> 2
> 2
> 3
> And there is variable for instance called -x- with other values e.g.
> 0
> 1
> 2
> 5
> -3
> 3
> What I want is a new variable which has on index i the counts
> of -x-'s smaller than the -limit- on that index. So for index
> 1 I want to count the number of values which are smaller than
> limit[1] = 2. In this case values 0,1 and -3 of -x- are
> smaller, so I want to have 3 on index 1 in my new variable.
> For index 2, only values 0 and -3 are smaller than
> limit[2]=1, so I want a 2 there.
> I tried summing over a variable with ones which didn't seem to work...
> (for instance: gen nsmall = sum(ones) if x < limit[_n])

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