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]

AW: Re: AW: st: Creating local from levelsof exceeding 255 characters


From   "Palan, Stefan ([email protected])" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   AW: Re: AW: st: Creating local from levelsof exceeding 255 characters
Date   Wed, 26 Jan 2011 11:58:25 +0100

Hi Maarten,

Sorry it took me a while, but I wanted to thank you profusely for your help - this finally did it!

Best regards,
Stefan.

----

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

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: 
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

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

http://www.maartenbuis.nl
--------------------------

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