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

Re: st: from variable to macro

From   "Nick Cox" <>
To   <>
Subject   Re: st: from variable to macro
Date   Mon, 18 Nov 2002 13:47:29 -0000

Shige Song wrote

> I'd like to know how to generate a macro out of a numerical
variable. For
> example, I have a variable A that includes 200 values, I want to
generate a
> macro X that contains all the values that variable A has so that  I
can do
> something like:

> foreach x of local X {

> qreg ... , q(x)

> }

Check out -levels- from SSC.

Here is what the author wrote:

-levels- serves two different functions.
Occasionally, it serves to give a compact
display of the distinct values of varname.
More commonly, it may be useful when it is
desired to cycle through the distinct values of varname
with (say) foreach.  -levels- leaves behind a list
in r(levels) which may be used in a subsequent command.

Note that -levels- won't work on a numeric
variable with fractional parts.

There is an older command, -vallist- (STB-60).
I recommend -levels- over -vallist-. -levels-
is considered by the author as -vallist-
done better.


*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index