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   Nick Cox <>
Subject   Re: st: labvarch-like command for value labels
Date   Mon, 9 May 2011 09:28:07 +0100

Thanks for the compliment.

Eric's answer is more or less what I would have written.

Most changes I make to value labels are at least a little ad hoc, so I
tend to fire up -label define- directly, or use -edit- or -varm-.

Something more systematic I would be likely to solve within a
-foreach- or -forval- loop,  possibly using extended macro functions.


On Sun, May 8, 2011 at 9:54 PM, Eric Booth <> wrote:

> 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

> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index