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]

st: RE: RE: RE: Changing variable name to variable label

From   "Martin Weiss" <>
To   <>
Subject   st: RE: RE: RE: Changing variable name to variable label
Date   Mon, 12 Apr 2010 23:17:34 +0200


" But I've not thought seriously about everything you need to do."

One of the hazards here is the possibility that variable label and variable
name coincide, as is the case for "race":

sysuse nlsw88, clear

foreach v of var _all{ 
	if "`: var label `v''" != ""  /// 
	rename `v' `=strtoname("`:var label `v''")'

Probably need to -capture- the whole thing...


-----Original Message-----
[] On Behalf Of Nick Cox
Sent: Montag, 12. April 2010 22:36
Subject: st: RE: RE: Changing variable name to variable label

That "seem to be constant" isn't nearly as firm as is needed. There
could be lots of small problems here and only an experienced Stata user
is well advised to start trying this. For example, small differences in
case, spacing, wording, etc. will make this much more complicated than
you might hope.

That said, you could run this code in each dataset. 

foreach v of var * { 
	local vlbl : var label `v' 
	if `"`vlbl'"' != "" { 
		local newname = strtoname(`"`vlbl'"') 
		rename `v' `newname' 

But I've not thought seriously about everything you need to do. Keep a
copy of all datasets unchanged. 


Martin Weiss

help mf_strtoname

comes to mind...

Daniel Jones

I have a set of repeated cross sections (10 years) which I would like to
combine into a single dataset. The problem is that the variable names
different across years -- it's a survey, so a variable I need might be
called "Q13" one year and "Q17" the next. 

Variable labels seem to be constant across years, so I thought a
might be to rename the the variables on the basis of their labels. I
found a
previous Statalist posting which describes such a procedure. However,
this to work, variable labels need to already be appropriately formatted
be variable names -- i.e., no spaces, etc. My variable labels are of the
form "Education Volunteer - Past 12 Months." I'm assuming there's a way
remove spaces, etc. from variable labels, and then make them the
names -- but I don't know what it is.

Alternatively, it seems like there could be a simpler solution to
all of these years together. Does anyone have any suggestions?

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index