Bookmark and Share

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

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

Re: st: Creating local from levelsof exceeding 255 characters

From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Creating local from levelsof exceeding 255 characters
Date   Fri, 14 Jan 2011 09:17:55 +0000 (GMT)

--- 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)?

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.

*-------------------- 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: )

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen


*   For searches and help try:

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