Ric,
Using the auto data set as an example, suppose your data file (data.dta)
consists of the variables: price, mpg, trunk, turn, and weight.
The label file consists of the variable names and new labels and it is also
a Stata data file (labels.dta) and looks like:
. l
+----------------------------+
| var1 var2 |
|----------------------------|
1. | mpg Miles per gallon |
2. | price Price in dollars |
3. | trunk Size of Trunk |
4. | turn Turn Radius |
5. | weight Weight of Vehicle |
+----------------------------+
One way to attach these new labels to the variables in the data file is as
follows:
. use "C:\Documents and Settings\Scott_2\Desktop\data.dta", clear
(1978 Automobile Data)
*alphabetizes the variables
. aorder
. save ,replace
file C:\Documents and Settings\Scott_2\Desktop\data.dta saved
. use "C:\Documents and Settings\Scott_2\Desktop\labels.dta", clear
(1978 Automobile Data)
*Sort the variable names in the label file
. sort var1
*Merge the data file to the labels file
. merge using "C:\Documents and Settings\Scott_2\Desktop\data.dta"
*Now the first 5 observations look like:
. cl var1-weight in 1/5, noobs
var1 var2 mpg price trunk turn weight
mpg Miles per gallon 22 4,099 11 40 2,930
price Price in dollars 17 4,749 11 40 3,350
trunk Size of Trunk 22 3,799 12 35 2,640
turn Turn Radius 20 4,816 16 40 3,250
weight Weight of Vehicle 15 7,827 20 43 4,080
*Cycle through the variables and pick up the new label from var2
. local i = 1
. foreach var of varlist mpg - weight {
2. local label = var2[`i']
3. label var `var' "`label'"
4. local i = `i' + 1
5. }
*Now, the new labels are attached.
. desc mpg-weight
storage display value
variable name type format label variable label
----------------------------------------------------------------------------
mpg int %8.0g Miles per gallon
price int %8.0gc Price in dollars
trunk int %8.0g Size of Trunk
turn int %8.0g Turn Radius
weight int %8.0gc Weight of Vehicle
. drop var1 var2 _merge
Hope this helps,
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Eric Uslaner
> Sent: Thursday, November 11, 2004 5:13 PM
> To: [email protected]
> Subject: st: Re: ST: transferring labels
>
> Hello,
>
> I have downloaded a data set that is in 3 parts, all in Excel. Files 2
> and 3 are data, file 1 is a list of variable names and labels. I have
> transferred the data with names to Stata, but the labels are perplexing.
> Here is the format of the Excel spreadsheet with the variable names
> and labels:
>
> Column A Column B Column C Column D Column E
> varname varlabel divider varname varlabel
> etc. etc. etc.
> etc.
>
>
> Columns A&B are for data set 2 and C & D for data set 3 (though this is
> no problem--I can copy them into one column since the cases are
> identical).
>
> Is there a way to transfer the variable labels without doing a lot of
> typing. Stata Technical Support suggested putting each variable label
> into a do-file with quotation marks around each label (which I would
> have to type in by hand) and then pasting:
>
> label var varname "varlabel" for each variable.
>
> Does anyone have a more expeditous (and less tedious) way of doing this
> transfer?
>
> Thanks,
>
> Ric Uslaner
> [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/