Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Transforming numeric to categorical (not to string!)


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Transforming numeric to categorical (not to string!)
Date   Thu, 12 Aug 2004 11:24:12 +0100

Tewodaj Mogues

> Is there a way to change the
> name of the label title? In your example, the variable name 
> is COLOUR, the
> label title is COLOURS, and the value labels are GREEN, WHITE, etc.
> Suppose I wanted to rename the label title to SHADES from 
> its original
> name COLOURS, but retain all the value labels as are. Is 
> there a way to do that that?

Clive Nicholas

> I don't really understand why you would need to do that. Certainly,
> there's nothing I can see in the User's Guide ([U] 15.6.3 and 
> 16.9) that
> would allow you to change the name created after -label 
> define-. If you
> don't have lots of -label-s stored in your dataset (-label 
> list- allows
> you to check this), the only solution I'd suggust is:
> 
> . label drop _all
> . label define shades 1 green 2 white 3 blue (or whatever)
> . label values colour shades

Like Clive, my main reaction was why do you want to do 
that? But still, there is a way. 

. findit label 

points to (among other things) a ragbag of label stuff
in the -labutil- package, some of which must look pretty
weird even to the author. But they suggest one way. 

Suppose you 

. sysuse auto, clear
. label dir
origin

and you desire to change this label name to -origin-. 
You can clone the set of value labels -origin- 
using -labvalclone- (-lab val clone-) 

. labvalclone origin foreign

and it's there:  

. label dir
foreign
origin

Naturally, this is trivial in the case of a set of 
value labels with two labels, for which Clive's 
solution above of just typing it out is much more 
sensible. But it's not trivial with a bigger set. So now you 
drop the label set you don't want: 

. label drop origin

However, there is likely to be another part to 
the problem. Almost certainly, that set of
value labels -origin- was associated with some 
variables, and Stata still thinks that is the case: 
it is merely that those labels don't exist. You 
presumably want to associate the newly renamed 
set of labels with those variables. In the case
of the auto data, you can do this directly 
in the standard way, as Clive pointed out: 

. label val foreign foreign 

If several variables were involved, you would
want a way of doing it more concisely (and more
reliably). -ds- offers a way to get a list 
of all the variable names concerned: 

. ds, has(vallabel origin)
foreign

and the variable list is left behind in `r(varlist)'.

This use of -ds- depends on your having updated
Stata 8 to at least 17 June 2003. 
 
So at most it is a three-line loop, even with 
tens or hundreds of variables with the same 
set of value labels. 

. foreach v in `r(varlist)' { 
  2.         label val `v' foreign
  3. }

. tab foreign

   Car type |      Freq.     Percent        Cum.
------------+-----------------------------------
   Domestic |         52       70.27       70.27
    Foreign |         22       29.73      100.00
------------+-----------------------------------
      Total |         74      100.00

Nick 
n.j.cox@durham.ac.uk 

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