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: st: How to extract text from Labels


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: How to extract text from Labels
Date   Wed, 6 Jun 2012 12:16:17 +0100

local first = strpos(`"`label"', "[") + 1

should be 

local first = strpos(`"`label'"', "[") + 1

        label var `v' `"`newlabel"'

should be 

        label var `v' `"`newlabel'"'

but I trust you spotted that. 

Nick 
[email protected] 


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: 05 June 2012 18:00
To: [email protected]
Subject: Re: st: How to extract text from Labels

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 <[email protected]> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index