Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: labvarch-like command for value labels


From   Eric Booth <ebooth@ppri.tamu.edu>
To   "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: labvarch-like command for value labels
Date   Sun, 8 May 2011 20:54:13 +0000

<>
Dimitriy:

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"'
	continue
	}
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
end


******EXAMPLES******

clear
discard
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
ebooth@ppri.tamu.edu
Office: +979.845.6754
Fax: +979.845.0249
http://ppri.tamu.edu

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.
> 
> DVM
> *

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?
> 
> DVM
> *




*
*   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   |   Site index