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

Re: st: levelsof

From   Keith Dear <>
Subject   Re: st: levelsof
Date   Fri, 21 Sep 2007 14:39:00 +1000

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"'.

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.

*   For searches and help try:

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
CRICOS provider #00120C

*   For searches and help try:

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