Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How can I put the numerical values of a variable into a numlist?


From   Michael Norman Mitchell <[email protected]>
To   [email protected]
Subject   Re: st: How can I put the numerical values of a variable into a numlist?
Date   Tue, 11 May 2010 09:09:22 -0700

Dear Nick

  Thanks for noting the limits of my answer! Indeed,

. levelsof id, local(a)

   is not only a more robust solution, but is is also simpler.

Thanks!

Michael N. Mitchell
See the Stata tidbit of the week at...
http://www.MichaelNormanMitchell.com

On 2010-05-11 4.31 AM, Nick Cox wrote:
-levelsof- is the appropriate command. However, the way Michael uses it
here leads to the right answer for very simple examples but otherwise
could easily bite you by giving an incomplete answer. Consider this
example:

. clear

. set obs 100
obs was 0, now 100

. gen id = _n

. levelsof id
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
52 53 54 55 56 57 58 59
  60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

. local a = r(levels)

. di "`a'"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
52 53 54 55 56 57 58 59
  60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
83 84

What happened to the list of identifiers? Why is it truncated?

The use of the = sign implies an _evaluation_ of the following
expression, here r(levels). But there is a limit on the length of the
expression that Stata can handle. This is documented at [U] 18.3.4
Macros and expressions and also in

SJ-8-4  pr0045  . . . . . . . . Stata tip 70: Beware the evaluating
equal sign
         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
         Q4/08   SJ 8(4):586--587                                 (no
commands)
         tip explaining the pitfall of losing content in a macro
         because of limits on the length of string expressions

There are two better ways to proceed. The first is just to copy, as no
evaluation is needed anyway:

. local a `r(levels)'

The second is use -levelsof-'s -local()- option.

. levelsof id, local(a)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
52 53 54 55 56 57 58 59
  60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

. di "`a'"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
52 53 54 55 56 57 58 59
  60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

I'd always recommend this second way.

Nick
[email protected]

Michael Norman Mitchell

    Does this example help?

. sysuse auto
(1978 Automobile Data)

. levelsof mpg
12 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 34 35 41

. local a = r(levels)

. di "`a'"
12 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 34 35 41


On 2010-05-10 10.43 PM, A Loumiotis wrote:

Is there any way to put the numerical values of a variable into a
numlist beside doing it manually?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index