Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using a codebook generated by "describe, replace" to label another dataset

    Hi

    I have built a readable codebook using the describe command as follows:

    Code:
    use "$mastData/Dataset1.dta", clear
    describe, replace
    save "$mastData/Codebook.dta", replace
    Is there an easy way to use the codebook to label variables with the same name in another dataset? Likewise, to use the codebook to apply labels to values?

    I can write something to label the variables and labels in a new file (Dataset2.dta) with those of the original file (Dataset1.dta), but I don't think it's the most efficient way & want to know if I'm missing an easier way/command.

    Code:
    * save val and var labels
    use "$mastData/Dataset1.dta", clear
    label save using "$mastData/_vallabels", replace
    describe, replace
    save "$mastData/Codebook.dta", replace
    
    * write label do file
    use "$mastData/Codebook.dta", clear
    // dofile
    tempname fh
    local N = c(N)
    // create a new do-file
    file open `fh' using "$mastData/_labels.do", write replace
    forvalues i = 1/`N' {
        // variable labels
        file write `fh' "cap label variable `= name[`i']' "
        file write `fh' `""`= varlab[`i']'""' _newline
        // value labels
        file write `fh' "cap label values `= name[`i']' "
        file write `fh' `""`= vallab[`i']'""' _newline    
    }
    file close `fh'
    
    * label new dataset
    use "$mastData/Dataset2.dta", clear
    do "$mastData/_vallabels"
    do "$mastData/_labels.do"
    Thank you for any help!


  • #2
    You can create a .do file with the value labels using label save. So that would be a start. After that I would just use the extended macro functions to extract the value and variable labels.

    Code:
    clear all
    
    program define copy_alllabels
        syntax , SAVing(string asis) [replace]
        label save using `saving', `replace'
    
        tempname fh
        file open `fh' using `saving', write append
        foreach var of varlist * {
            local lab : value label `var'
            if `"`lab'"' != "" {
                file write `fh' `"label value `var' `lab'"' _n
            }
            local lab : variable label `var'
            if `"`lab'"' != "" {
                file write `fh' `"label variable `var' `"`lab'"' "' _n
            }
        }
        file close `fh'
        doedit `saving'
    end
    
    sysuse nlsw88, clear
    copy_alllabels, saving(c:\temp\foo.do) replace
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      elabel (SSC) has an undocumented extended option for the label language subcommand. Here is an example:

      Code:
      . sysuse auto , clear
      (1978 Automobile Data)
      
      . 
      . // save the label language
      . tempfile tmp
      
      . elabel language default , saving("`tmp'")
      file [ ... ]\Temp\ST_21b4_000002.tmp saved
      
      . 
      . // show what is saved
      . type "`tmp'"
      label language default, new
      label data `"1978 Automobile Data"'
      label variable make `"Make and Model"'
      label variable price `"Price"'
      label values price 
      label variable mpg `"Mileage (mpg)"'
      label values mpg 
      label variable rep78 `"Repair Record 1978"'
      label values rep78 
      label variable headroom `"Headroom (in.)"'
      label values headroom 
      label variable trunk `"Trunk space (cu. ft.)"'
      label values trunk 
      label variable weight `"Weight (lbs.)"'
      label values weight 
      label variable length `"Length (in.)"'
      label values length 
      label variable turn `"Turn Circle (ft.) "'
      label values turn 
      label variable displacement `"Displacement (cu. in.)"'
      label values displacement 
      label variable gear_ratio `"Gear Ratio"'
      label values gear_ratio 
      label variable foreign `"Car type"'
      label values foreign origin
      label define origin 0 `"Domestic"', modify
      label define origin 1 `"Foreign"', modify

      The underlying code is very similar to what Megan and Maarten have proposed. I think this is a very unusual situation and it would be interesting to know how it did arise for Megan.

      Comment

      Working...
      X