Statalist


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

Re: AW: st: AW: AW: string functions


From   Miranda Kim <mk@mrc.soton.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: AW: st: AW: AW: string functions
Date   Mon, 26 Oct 2009 14:12:04 +0000

Exactly what I was after, thank you so much !!!

Martin Weiss wrote:
<>
Version resistant to labels without _or with inner_ opening
brackets/parantheses

*************
clear*

set obs 1
gen myvar1=.
gen myvar2=.
gen myvar3=.
gen myvar4=.
la var myvar1 "area (cm sq)"
la var myvar2 "other area (cm sq)"
la var myvar3 "other area"
la var myvar4 "other (cm sq) area"

desc

qui ds, has(varlabel)

foreach var of varlist `r(varlist)'{
	local mylabel="`:var label `var''"
	if strpos("`mylabel'","(")!=0{
local mynewlabel= /* */ substr("`mylabel'", 1, strpos("`mylabel'","(")-1) /* */ +substr("`mylabel'", strpos("`mylabel'",")")+1,. )
		la var `var' "`mynewlabel'"
	}
}

desc
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Miranda Kim
Gesendet: Montag, 26. Oktober 2009 15:00
An: statalist@hsphsun2.harvard.edu
Betreff: Re: st: AW: AW: string functions

Thanks a lot for your responses Nick and Martin,
I am only removing the units from labels when it is appropriate to do so (for example when creating a z-score and using the original variable label to create a new label for the standardised variable). Is there a generic way of removing the bracketed units even if they are not placed at the end of the label, for example "total vit A intake (mg/day), before pregnancy". Note that I have over fifty different units (some are 1 word in length, some 2 words and some 3 due to spaces in the brackets) and I was hoping to not have to list them all using the subinstr() function.
Many thanks for your suggestions!

Martin Weiss wrote:
<>
Version resistant to labels without opening brackets/parantheses


*************
clear*

set obs 1
gen myvar1=.
gen myvar2=.
gen myvar3=.
la var myvar1 "area (cm sq)"
la var myvar2 "other area (cm sq)"
la var myvar3 "other area"

qui ds, has(varlabel)

foreach var of varlist `r(varlist)'{
	local mylabel="`:var label `var''"
	if strpos("`mylabel'","(")!=0{
local mynewlabel= /* */ substr("`mylabel'", 1, strpos("`mylabel'","(")-1)
		la var `var' "`mynewlabel'"
	}
}
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Martin Weiss
Gesendet: Montag, 26. Oktober 2009 13:38
An: statalist@hsphsun2.harvard.edu
Betreff: st: AW: string functions


<>


*************
clear*

set obs 1
gen myvar1=.
gen myvar2=.

la var myvar1 "area (cm sq)"
la var myvar2 "other area (cm sq)"

desc

qui ds, has(varlabel)

foreach var of varlist `r(varlist)'{
	local mylabel="`:var label `var''"
	local mynewlabel=substr("`mylabel'", 1, strpos("`mylabel'","(")-1)
	la var `var' "`mynewlabel'"
}

//check success
desc
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Miranda Kim
Gesendet: Montag, 26. Oktober 2009 13:28
An: statalist@hsphsun2.harvard.edu
Betreff: st: string functions

Hello,
I have a basic question on using string functions to do the following:
I have variable labels that have various units in brackets: (cm) (cm sq) etc. and I want to remove these from the label. How can I search a string for the portion between brackets (including the brackets) and replace this with a blank? For example if my variable area has the following label: "area (cm sq) of ..."
I want to replace the label with : "area of ..."
I have many variables so I want to automate this rather than having to type in individual labels...
I use stata v10
Many thanks!


*
*   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/


*
*   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/


*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index