Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: levelsof


From   Keith Dear <keith.dear@anu.edu.au>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: levelsof
Date   Fri, 21 Sep 2007 14:39:00 +1000

Luhang,
Read -help levelsof-. You need to name, as an option, the local macro where you want the list to be stored, thus:
levelsof YY, loc(levels)
where "levels" is your choice of name for a local macro.

Alternatively, you can access the result of -levelsof- as r(levels), like this:
use Y, clear
levelsof YY
use X, clear
foreach l in `=r(levels)' {
replace XX=subinstr(XX,"`l'","",.)
}
Note that foreach needs "in" not "of" in this usage, because there is no keyword (such as "local"), simply a list of terms. In your example, `=r(levels)' evaluates to the list `"a"' `"b"'.
kd

At 01:17 PM 21/09/2007, you wrote:

Dear all,

I'm trying to take some strings out of a string variable. Suppose data
set Y contains the list of strings, named variable YY, that I want to
take out from a string variable XX in data set X. An example is that
YY takes value "a" and "b", and XX takes "ac" and "bd", the results I
want are "c" and "d".

My code is as following:

***************************************************
use Y,clear
levelsof YY

use X,clear
foreach l of local levels {
           replace XX=subinstr(XX,"`l'","",.)
}

**************************************************
It doesn't work. Could someone tell me what is wrong with my code? Or
is there better method achieving this? Many thanks in advance.

Cheers,
Luhang
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

Dr Keith B.G. Dear
Senior Fellow in Biostatistics
National Centre for Epidemiology and Population Health
Australian National University
Canberra, ACT 0200, Australia
Tel: 02 612 54865, Fax: 02 612 50740
http://nceph.anu.edu.au/Staff_Students/staff_pages/dear.php
CRICOS provider #00120C

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index