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]
AW: st: How can I put the numerical values of a variable into a numlist?
From
"Martin Weiss" <[email protected]>
To
<[email protected]>
Subject
AW: st: How can I put the numerical values of a variable into a numlist?
Date
Tue, 11 May 2010 13:49:23 +0200
<>
Nick`s own FAQ at http://www.stata.com/support/faqs/data/foreach.html may
also be helpful to Antonis.
HTH
Martin
-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Dienstag, 11. Mai 2010 13:31
An: [email protected]
Betreff: RE: st: How can I put the numerical values of a variable into a
numlist?
-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/