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   TEWODAJ MOGUES <[email protected]>
To   Stata _ <[email protected]>
Subject   RE: st: Transforming numeric to categorical (not to string!)
Date   Fri, 13 Aug 2004 10:07:02 -0400

Clive and Nick,

The reason I wanted to change label names is: Sometimes I first arbitrarily choose label names, and later, as I create other sets of labels and other variables, realise that giving label names in a more systematic fashion makes datawork easier (e.g. renaming "food" to "grains" may be more appropriate for a set of labels of crop types, once I have created another set of labels with generic food types which I now want to call "food"). Plus, I want to create consistency of label names across datasets where they once were not consistent, etc. 

Thanks to both of you for your tips. Clive, I didn't really want to change value labels, but rather keep them the same and change the label (you forgot - 6 "Libertarian yellow" - at least using the German political colour-code).

Nick, your suggestions helped perfectly. -labvalclone- is really great, and -ds,has()- is very useful if I have multiple vars with the same label that I want to change. I don't now but may in the future. -labvalclone- would be especially helpful if I could this way carry over a set of labels from one dataset to another (without having to merge a variable that's in one dataset and has the desired labels, into the other dataset). That is, I have in dataset A the label name COLOURS with which are associated the value labels red, libertarian-yellow, and blue, and which I use for the variable PARTY_SHADES. Now I want to use COLOURS for the variable HUES which is located in dataset B. Can I somehow transfer labels across datasets? So far I have been creating the label name and all value labels all over again in the new dataset.

Tewodaj

 
-------------------------
Original message:
   Date: Thu, 12 Aug 2004 11:24:12 +0100
   From: "Nick Cox" <[email protected]>
Subject: RE: st: Transforming numeric to categorical (not to string!)

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 
[email protected] 

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index