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

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

--- On Fri, 14/1/11, Palan, Stefan wrote:
> What I do is, I fill my local "a" with the levelsof some
> variable. The local a then contains some 100 numbers. But
> then, I need to create a local that contains a string that
> looks like "t1 t2 t4 t7...". In other words, it needs to
> contain an entry for each number in a, but with a leading
> "t", because my variable names are t1, t2, etc. 

One way to do this is to: 
1) make sure that each element in your list is separated by
one space (extended macro function -list retokenize-)

2) replace each " " with " t" (extended macro function 

3) this only leaves the first element without a t prefix, so 
add that initial t.

Putting this all together:

*--------------- 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(levs)

// makes sure that elements separated by one space
// this is probably superfluous since this is created
// by -levelsof-, but it does not hurt.
local levs : list retokenize levs

// replace space with space and "t"
local levs : subinstr local levs " " " t", all

// add the initial "t"
local levs "t`levs'"

// do the loop
foreach lev of local levs {
    di "`lev' " 
*--------------- 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