Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

Re: st: labvarch-like command for value labels

From   Eric Booth <>
To   "<>" <>
Subject   Re: st: labvarch-like command for value labels
Date   Sun, 8 May 2011 20:54:13 +0000


Nick Cox's -labvarch- is a part of the package -labutil- (from SSC).  There are other programs in that package, including -labvalch-, but that program doesn't do to value labels what you describe (based on the functionality of -labvarch-).  
I haven't seen other programs that do what you describe, but you could program it yourself.  Here's a quick & dirty version of a program (called labvalch2) similar to what you describe and that steals a sub-set of options from those offered by NJC in -labvarch- and applies them to value labels.  
This could be expanded to mirror the options available to -labvarch-, but the important thing to note is the use of macro extended functions (-help extended_fcn-) to manipulate the value labels -- you can use these same principals to add whatever functionality you desire to similar code:

*********************!  begin labvalch example
cap program drop labvalch2
program define labvalch2

syntax varlist(numeric) [, Upper Lower ///
	PREfix(str asis) POSTfix(str asis)]
foreach v in `varlist' {
**check for value labels:
loc _check: val l `v'
if `"`_check'"' == "" {
	di as error `"Variable `v' does not have val labels assigned"'
levelsof `v', loc(l_`v')
	foreach vv in `l_`v'' {
		loc `v'_`vv':label `_check' `vv'
foreach x in upper lower {
if `"``x''"' == "`x'" 	loc `v'_`vv' = `x'("``v'_`vv''")
if `"`prefix'"' != "" | `"`postfix'"' != "" {
	loc prefix:subinstr loc prefix `"""' "", all
	loc postfix:subinstr loc postfix `"""' "", all
	loc `v'_`vv'  `"`prefix' ``v'_`vv'' `postfix'"'
lab def `_check' `vv' `"``v'_`vv''"' , modify
} //end vv loop
} //end v loop


sysuse auto, clear
lab li origin
labvalch2 for, upper prefix( "Automobile ")
lab li origin
labvalch2 for, lower postfix( Car )
lab li origin
labvalch2 rep78, lower  //--will error out b/c no value labels for rep78

*********************! end labvalch example
- Eric

Eric A. Booth
Public Policy Research Institute
Texas A&M University
Office: +979.845.6754
Fax: +979.845.0249

On May 8, 2011, at 3:41 PM, Dimitriy V. Masterov wrote:

> I would like to change value labels themselves. For example, take the
> foreign variable from the auto dataset. Is there a way to add the word
> Automobile so that the value labels become "Foreign Automobile" and
> "Domestic Automobile" and then to replace "Automobile" with "Car"?
> These are trivial examples and strrec would definitely work if I was
> willing to type things out, but I would like to avoid that when there
> are many labels.
> *

On May 8, 2011, at 2:19 PM, Dimitriy V. Masterov wrote:

> I am a huge fan of Nicholas Cox's labvarch for adding postfixes and
> prefixes to variable labels. Does something like this exist for value
> labels?
> *

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index