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

st: RE: xi variable labels

From   "Nick Cox" <>
To   <>
Subject   st: RE: xi variable labels
Date   Tue, 15 Jul 2003 09:15:59 +0100

Dimitriy V. Masterov

> I've been having a problem with the variable labels created
> by the command
> xi. For example, I have a variable called race that takes
> on values 1-3.
> When I type "xi i.race" the new variables _Irace_2 and _Irace_3 are
> labeled "race==2" and "race==3". I would like them to be
> labeled with the
> value label from the original variable. For example, if the value
> race==2 was labeled "White" I would like the variable _Irace_2 to be
> labeled "White", and so on.  This would make the graphs
> that I have to
> make  a lot simpler.
> Is there a simple way of doing this somehow without doing
> it by hand?

I don't know the user-written alternatives -desmat- and -xi3-
well enough to know if they offer solutions.

An upstream fix would be better than the following downstream fix,
but this does improve the problem some of the time.

program def myrelabel
*! NJC 1.0.0 15 July 2003
	version 7
	syntax varlist(numeric)

	tokenize `varlist'
	local nvars : word count `varlist'
	local last ``nvars''
	local vallabel : value label `last'
	if "`vallabel'" == "" {
		di as err "`last' not labelled"
		exit 498

	local `nvars'
	local varlist "`*'"

	foreach v of local varlist {
		local varlabel : variable label `v'
		local eqs = index(`"`varlabel'"', "==")
		if `eqs' {
			local value = real(substr(`"`varlabel'"', `eqs' + 2, .))
			if `value' < . {
				local label : label `vallabel' `value'
				label var `v' `"`label'"'


Suppose, for example, you have a bunch of indicator


formed after some

xi: <whatever>

Then you go

myrelabel _Ifoo* foo

Think of this as

"relabel the variable labels of _Ifoo* according to the
value labels of foo"

Given a bunch of bunches, you could try

foreach v in foo bar bazz gould wiggins {
	myrelabel _I`v'* `v'

Note that this does ___not___ fix interaction terms.
The extension is left as an exercise.


*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index