Announcement

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

  • How to enter variable labels

    Dear Stata-users,

    I am dealing with multiple large datasets from various excel files requiring repeated matching / append processes. Here, in this case, I have a file A with two string variables var1 with names (string) and var2 with descriptions (string), as below:

    var1 (COLUMNNAME) ------ var2 ( COLUMNDESC)
    1. MCS21 ------ Please specify ~ Eyes
    2. MNPDSP2 ------ Please specify ~ Operator
    3. MBBPS11 ------ Lipid Laden macrophages
    etc , etc


    Also, I have a file B and a file C with different data and variables that are no labelled, however their labels are in the file-A

    e.g. file-B: MCS21 str48 %48s (unlabelled)

    e.g. file-C: MBBPS11 str48 %48s (unlabelled)


    My task is to instruct Stata using a loop to identify the varnames in file A (as in column var1 ) and attached the correct variable labels (as in column var2 ) into the variables of the files B and C.
    Any idea how this should be done?

    Thank you in advance
    George




  • #2
    Here is an example:

    Code:
    // create some example data
    clear
    tempfile a b c
    
    input str2 var str26 varlabel
    "x1"  "erg belangrijke informatie"
    "x2"  "doet er niet toe"
    "z1"  "kan belangrijk zijn"
    "z2"  "misschien ook niet"
    end
    save `a'
    
    drop _all
    set obs 10
    gen x1 = rnormal()
    gen x2 = rnormal()
    save `b'
    
    drop _all
    set obs 15
    gen z1 = rnormal()
    gen z2 = rnormal()
    gen z3 = rnormal()
    save `c'
    
    // collect the info from file a
    use `a', clear
    local n = _N
    forvalues i = 1/`n' {
        local var`i' = var[`i']
        local lab`i' = varlabel[`i']
    }
    
    // apply that to the other files
    use `b', clear
    forvalues i = 1/`n' {
        capture confirm variable `var`i''    // does that variable exist
        if !_rc {                            // if so, apply label
            label var `var`i'' `"`lab`i''"'
        }
    }
    desc                                     // admire the result
    
    use `c', clear
    forvalues i = 1/`n' {
        capture confirm variable `var`i''    // does that variable exist
        if !_rc {                            // if so, apply label
            label var `var`i'' `"`lab`i''"'
        }
    }
    desc                                     // admire the result
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X