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
Jörg Eulenberger <[email protected]>
To
[email protected]
Subject
Re: st: How to extract text from Labels
Date
Wed, 06 Jun 2012 08:40:33 +0200
Dear Nick,
it is being very usefull to learn more about extended macro functions
and string functions.
Thanks a lot,
Jörg
Am 05.06.2012 19:00, schrieb Nick Cox:
> 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/
>
*
* 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/