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: Creating local from levelsof exceeding 255 characters


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Creating local from levelsof exceeding 255 characters
Date   Fri, 14 Jan 2011 09:37:27 +0000

In addition to Maarten's answer, note that -levelsof- has a -local()-
option. You never _need_ to create it yourself using r(levels).

levelsof id, local(allvalues)

Nick

On Fri, Jan 14, 2011 at 9:17 AM, Maarten buis <maartenbuis@yahoo.co.uk> wrote:

>
> The trick is that a local can store much longer strings as
> long as you do not ask it to evaluate that string. That means
> that you tell it:
>
> local levs `"`r(levels)'"'
>
> instead of:
>
> local levs = `"`r(levels)'"'
>
> (the difference is the = sign)
>
> After that you cannot use the normal string functions to
> manipulate it, because then the string is evaluated again, and
> thus truncated. Instead you need to use the extended macro
> functions. The help file documenting the extended macro functions
> is called -help extended_fcn-, but somehow I cannot remember that,
> so I always type -help macro-, and than follow the link to the
> extended macro functions.
>
> Also see: Nicholas J. Cox (2008) "Stata tip 70: Beware the
> evaluating equal sign" The Stata Journal, 8(4):586-587.
> <http://www.stata-journal.com/article.html?article=pr0045>
>
> *-------------------- begin example -----------------------
> // create a variable such that a string listing
> // all its values is longer than 245 characters
> drop _all
> set obs 500
> gen id = _n
>
> // store that string in locals
> levelsof id
> local allvalues   `"`r(levels)'"'
> local truncated = `"`r(levels)'"'
>
> // display the number of characters using extended
> // macro functions, see -help extended_fcn-
> di `: length local allvalues'
> di `: length local truncated'
>
> // using string functions leads to truncation
> di length(`"`allvalues'"')
> di length(`"`truncated'"')
> *--------------------- end example ----------------------
> (For more on examples I sent to the Statalist see:
> http://www.maartenbuis.nl/example_faq )
>

> --- On Fri, 14/1/11, Palan, Stefan wrote:
>> I want to use the levelsof command to create a local macro.
>> Unfortunately, the result exceeds the 255 characters Stata
>> stores in a local macro. Is there some way I can circumvent
>> this? Do I have to split up the output of levelsof and save
>> in in two macros (and how do I do that)?
*
*   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