Announcement

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

  • Import variable labels and notes from one stata file to another

    Hi all

    I have two datasets A and B. B is the collapsed version of A. How can I import variables labels and variable notes from A to B?

    Thanks in advance

  • #2
    Something like this:

    Code:
    //    GET A LIST OF ALL VARIABLES IN B
    use B, clear
    unab vbles: _all
    
    //    EXTRACT THE LABELS AND CHARACTERISTICS
    //    OF THOSE VARIABLES IN DATA SET A
    use A, clear
    foreach v of varlist `vbles'  {
        local `v'_lab: var label `v'
        local `v'_charlist: char `v'[]
        foreach c of local `v'_charlist  {
            local `v'_`c': char `v'[`c']
        }
    }
    
    //    RETURN TO B AND APPLY THEM
    use B, clear
    foreach v of varlist `vbles' {
        label var `v' `"``v'_lab'"'
        foreach c of local `v'_charlist {
            char `v'[`c'] `"``v'_`c''"'
        }
    }
    Notes are characteristics, so they will be picked up, along with any other characteristics of these variables, by this code.

    ​​​​​​​This code is not tested and may contain typos, unbalanced quotes or braces, etc.


    Comment

    Working...
    X