[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: Importing variable (not value) labels

From   Eric Booth <[email protected]>
To   [email protected]
Subject   Re: st: RE: Importing variable (not value) labels
Date   Wed, 6 Jan 2010 12:52:13 -0600

Hi Dorothy:

Here's one way to do it:

*--------------BEGIN EXAMPLE
global sf "`pwd'"
This is your spreadsheet with the varnames & labels:

 inp str30 varname str50 varlabel
 "var1" "label1 has lots of words in it"
"var2" "label2"
"var3" "label3"
"var4" "label4"
"var5" "label5"
"variable2" "morelabels"
"variable" "labeled"
"var6" "label6"
"var7" "label7"
"var8" "label8"
"var9" "test"
"var99" "lastone"

This process will rename your vars/labels 
backward, so you need to pay special attention
to your sort may need to simply reverse
the order with -gsort-, or you might need to specify the
exact sort order with -sort-
So, the order of the vars/labels in your spreadsheet should
be in the reverse order of the vars in your target dataset
Also, the number of vars in your target dataset should match
the number of vars/labels in this spreadsheet
Finally, don't use dashes "-"  in your var labels
gsort -varname

//also, we need to get rid of spaced in the varlabel, for now
replace varlabel = subinstr(varlabel," ", "_", .)

levelsof varname, local(varname) clean
di "`varname'"  //<-- notice the order of the varnames
levelsof varlabel, local(varlabel) clean

//we are going to rename the vars in auto.dta
sysuse auto.dta, clear
local varlist `r(varlist)'
//make sure these line up:
di "`r(varlist)'" 
di "`varname'"  
tokenize `"`varname'"'
foreach v of local varlist {
	rename `v' `1'
	macro shift
local varlist `r(varlist)'
tokenize `"`varlabel'"'
foreach v of local varlist {
	label var `v' `1'
	macro shift

//replace the spaces in the varlabel//
	cap ssc install labutil   
	labvarch `varlist', subst("_" " ")

save "$sf/updated_data.dta", replace
use "$sf/updated_data.dta", clear
codebook, c
*--------------END EXAMPLE

~ Eric
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754

On Jan 6, 2010, at 12:05 PM, Nick Cox wrote:

> Yes, if you do it interactively, e.g. within the Data Editor, in which
> case -lab var- is still implicit. But I don't think that's what you're
> seeking, as that would be more work, not less. 
> Otherwise no. But you should be able to make it easier by a global edit
> of a text file containing the information in a decent text editor to
> produce a .do file. 
> Nick 
> [email protected] 
> Dorothy Bridges
> I need to label dozens of variables.  Is there a way to do this
> without using the "lab var" command for each one?  I have the variable
> names and labels in an excel (or .dta) file.

*   For searches and help try:

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