Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How to extract text from Labels


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: How to extract text from Labels
Date   Tue, 5 Jun 2012 18:00:02 +0100

Almost daily, or so it seems, there are questions to which the answers
are: use extended macro functions! use string functions! Here is
another.

Code something like this:

foreach v of varlist <yourlist> {
        local label : var label `v'
        local first = strpos(`"`label"', "[") + 1
        local second = strpos(`"`label'"', "]")
        local newlabel = substr(`"`label'"', `first', `second' - `first')
        label var `v' `"`newlabel"'
}

Or a regular expression approach

foreach v of varlist <yourlist> {
        local label : var label `v'
        if regexm(`"`label'"'", "\[(.*)\]") {
                local newlabel = regexs(1)
         }
         label var `v' `"`newlabel"'
}

-save- the dataset first before you experiment.

On Tue, Jun 5, 2012 at 4:50 PM, Jörg Eulenberger <j.eulenberger@web.de> wrote:

> I have a problem with labels.
>
> In my Dataset are single items (binary) from a multiple option question.
> The label format of this variables are “[Text1]Text2”. Now I want to
> relabel the variables only with the Text in the square brackets (Text1).
>
> Any ideas for a syntax that i can use in a foreach-loop?

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index