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 <n.j.cox@durham.ac.uk>
To
"'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
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
n.j.cox@durham.ac.uk
-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 05 June 2012 18:00
To: statalist@hsphsun2.harvard.edu
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 <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/