Statalist


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

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


From   Dorothy Bridges <[email protected]>
To   [email protected]
Subject   Re: st: RE: Importing variable (not value) labels
Date   Thu, 7 Jan 2010 17:11:02 -0500

Thanks, everyone.  Very helpful.

On Wed, Jan 6, 2010 at 2:13 PM, Eric Booth <[email protected]> wrote:
> <>
>
> By the way, I just ran across Roger Newson's helpful utility -descsave- which can help with going the opposite direction with your issue (exporting some variable names & labels to apply to another dataset).
>
> e.g.,
> **
> ssc install descsave
> sysuse auto, clear
> descsave * , saving( test.dta , replace) keep(name varlab) gsort(name)
> use test.dta, clear
> **
>
> ~Eric
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> [email protected]
> Office: +979.845.6754
>
>
>
> On Jan 6, 2010, at 12:52 PM, Eric Booth wrote:
>
>> <>
>> Hi Dorothy:
>>
>> Here's one way to do it:
>>
>> *--------------BEGIN EXAMPLE
>> global sf "`pwd'"
>> /*
>> This is your spreadsheet with the varnames & labels:
>> */
>>
>> clear
>> 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"
>> end
>>
>> /*
>> This process will rename your vars/labels
>> backward, so you need to pay special attention
>> to your sort order...you 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
>>
>> **
>> preserve
>> //we are going to rename the vars in auto.dta
>> sysuse auto.dta, clear
>> ds
>> 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
>>       }
>> ds
>> 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
>> restore
>> **
>> use "$sf/updated_data.dta", clear
>> desc
>> 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:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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