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

RE: st: loop thru unique values of a variable


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: loop thru unique values of a variable
Date   Tue, 10 Sep 2002 10:12:04 +0100

Roger Newson
>
> At 14:49 09/09/02 +0100, Nick Cox wrote:
> >I consider -vallist- (STB-60, updated on SSC) to be superseded
> >by -levels-
> >(SSC). Loosely speaking, -levels- does less, but it marks a retreat
> >to the main idea behind -vallist- and is designed to work more
> >obviously
> >and more smoothly with -foreach-. The difference may seem
> >small from a translation of Roger's example
> >
> >levels D, local(values)
> >foreach d of local values {
> >         ...
> >}
> >
> >but there are good reasons why this is slightly better style.
>
> Except if D has non-integer values, in which case (as far
> as I can see) you
> have to use -vallist-.
>

Correct.

My -levels- (SSC) has two functions. The lesser
role is to present a concise list
of the distinct values of its argument, so that

. levels rep78

displays

1 2 3 4 5

The greater role is to provide an argument
for -foreach- making it easier to go

. levels rep78, local(values)

. foreach v of local values {
	...
}

-levels- will work with numeric
variables taking on integer values and with string variables,
up to the limits imposed by Stata. Such variables
might be called categorical variables in one broad
sense.

As Roger says, -vallist- (STB-60, later version on
SSC) is broader-minded, and lets you ask for the
values of a numeric variable taking on non-integer values.
The problems that can arise may can be illustrated by

. vallist gear_ratio

2.190000057220459 2.240000009536743 2.259999990463257 ...

This is the classic precision problem arising from the
fact that numbers must be held in binary. There are
work-arounds to this, both within -vallist- and
outside it, but I decided that it was a complication
too far, because sooner or later someone would be bit
when they tried to loop around a series of statements
of the form

... if <varname>==<value>

without being sufficiently circumspect. As it happens,
I have not ever wanted to use -foreach- to cycle over a variable
like -gear_ratio-, but it comes down to a matter of
programming taste.

That said, Nick Winter has just identified a small
bug in -levels- for a particular combination
of options, which I have fixed. I will ask Kit
Baum to put the revised version up on SSC.

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

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