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


From   Roger Newson <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: AW: st: Creating local from levelsof exceeding 255 characters
Date   Fri, 14 Jan 2011 14:48:36 +0000

I personally usually use Mata in cases similar to this. See the on-line help under

help mf_st_local

for more on the -st_local()- function (which moves values between Local macros and string scalars), and under

help mf_st_global

for more on the -st_global()- function (which moves values between -r()- results and string scalars), and under

help mf_tokens

for more on the -tokens()- function (which converts a string list to a string vector), and

help mf_invtokens

for more on the -invtokens()- function (which converts a string vector to a string list).

I hope this helps.

Best wishes

Roger


Roger B Newson BSc MSc DPhil
Lecturer in Medical Statistics
Respiratory Epidemiology and Public Health Group
National Heart and Lung Institute
Imperial College London
Royal Brompton Campus
Room 33, Emmanuel Kaye Building
1B Manresa Road
London SW3 6LR
UNITED KINGDOM
Tel: +44 (0)20 7352 8121 ext 3381
Fax: +44 (0)20 7351 8322
Email: [email protected]
Web page: http://www.imperial.ac.uk/nhli/r.newson/
Departmental Web page:
http://www1.imperial.ac.uk/medicine/about/divisions/nhli/respiration/popgenetics/reph/

Opinions expressed are those of the author, not of the institution.

On 14/01/2011 14:22, Maarten buis wrote:
--- 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/
*
*   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