Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Change value labels to lowercase


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Change value labels to lowercase
Date   Thu, 6 Apr 2006 00:32:03 +0100

That was premature. 

I wrote a program to change all the value labels 
in a set of value labels to lower case. Here it
is. It is just a variation on -labvalclone- in 
-labutil- on SSC. 

*! NJC 1.0.0 6 April 2006 
program labvallc
	version 8 
	args old new garbage 
	if "`old'" == "" | "`new'" == "" | "`garbage'" != "" { 
		di as err "syntax is: " ///
		as txt "labvallc {it:vallblname newvallblname}" 
		exit 198 
	} 
	if "`old'" == "label" | "`old'" == "define" { 
		di as err "won't work if {txt:`old'} is existing value label name" 
		exit 198 
	}	
	capture label list `new' 
	if _rc == 0 { 
		di as err "value labels {txt:`new'} already exist" 
		exit 198
	} 
	
	tempfile file1 file2
	tempname in out 

	qui label save `old' using `"`file1'"' 
	file open `in' using `"`file1'"', r
	file open `out' using `"`file2'"', w
	file read `in' line
	
	while r(eof) == 0 {
		local line: subinstr local line "`old'" "`new'"
		local line = lower(`"`line'"') 
		file write `out' `"`line'"' _n
		file read `in' line
	}
	file close `out'
	
	qui do `"`file2'"'   
end 

. label def upper 1 STATA 2 IS 3 THE 4 NAME 
 
. labvallc upper lower
 
. lab li lower
 lower:
            1 stata
            2 is
            3 the
            4 name
 
 . lab li upper
 upper:
            1 STATA
            2 IS
            3 THE
            4 NAME

But on a second reading that is not your problem. 
Although you don't quite say so, it seems you want to 
change the _name_ of each set of value labels and 
not the labels themselves. 

Well, I think that is this: 

ds, has(vall) 
local V "`r(varlist)'" 

foreach v of local V { 
	local u : value label `v' 
	local l : lower("`u'") 
	capture labvalclone `u' `l' 
	if _rc == 0 { 
		label val `v' `l' 
		label drop `u' 
	}
}

but that's not tested. 
	
Nick 
n.j.cox@durham.ac.uk 

Friedrich Huebler
 
> > I want to change the value labels in my data from uppercase to
> > lowercase. Assume I have data like this:
> > 
> > variable     value
> > name         label
> > -------------------
> > var019
> > var020       VAR020
> > var021
> > var022
> > var023       VAR023
> > var024       VAR024
> > 
> > Variable names are all lowercase. Value labels are the same as the
> > variable names but are all uppercase. How can I loop through all
> > variables, identify those that have a value label, and change that
> > value lable to lowercase?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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