Statalist


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

Re: st: levelsof


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: levelsof
Date   Fri, 21 Sep 2007 09:47:10 +0200

This reminds me to place a wish regarding levelsof: I wish I had an
extended marco function  -levelsof-, so that I could code things like

forach k in `:levelsof country' {
 ...
}

or

egen axis = axis(price), label(make)
graph twoway scatter axis weight, ylab(`:levelsof axis', valuelabel)


Uli


Am Freitag, den 21.09.2007, 14:39 +1000 schrieb Keith Dear:
> 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/

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index