  # st: RE: -levels- question

 From "Nick Cox" To Subject st: RE: -levels- question Date Thu, 18 Sep 2003 18:24:18 +0100

```Danielle H. Ferry

> Using the example provided in the documentation of -levels-:
>
>     . levels factor, local(levels)
>     . foreach l of local levels {
>     .        di "-> factor = `l'"
>     .        whatever if factor == `l'
>     . }
>
> Is it possible to display the value labels of factor rather than the
> numerical codes?

Good question.

. levels factor, local(levels)
. foreach l of local levels {
.        di `"-> factor = `: label (factor) `l''"'
.        whatever if factor == `l'
. }

Let's unpack this. What is in the documentation
displays

"-> factor = `l'"

so Stata looks inside and substitutes the current
value for `l'. In this variant we ask for a display
of

`"-> factor = `: label (factor) `l''"'

As in elementary algebra, with nested ` ' we
should go as deep as we can and work outwards,
as that is what Stata will do. Deepest is `l',
as before. Suppose in an example it is 1.
Stata now works on

`: label (factor) 1'

which is the value label for the variable
-factor- when the value is 1.

Finally, the surrounding compound double quotes
`" "' are a protection against the possibility
that the value label itself contains double
quotes. Often you can be cavalier and go

"-> factor = `: label (factor) `l''"

This is all documented in Stata 8 and implemented
but not documented in Stata 7. The on-line help
is -help macro-. There is more at [P] macro
and somewhere in [U].

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