Announcement

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

  • Merging two datasets with overlapping value label names

    I have two datasets that were initially SPSS datasets. I imported them and saved them as .dta and now want to merge them. When I imported them the value labels got defined as labels0, labels1, labels2 etc. These now exist in both datasets but they describe different values. So, for example in dataset 1, labels0 stands for: 1 Male and 2 Female, but in dataset 2, labels0 might stand for 1 Agegroup1, 2 Agegroup2, 3 Agegroup3 and 4 Agegroup4. When I merge both datasets, Stata tells me
    (label labels0 already defined)
    (label labels1 already defined) etc.
    and uses the labels from the first dataset for the using dataset, which leads to incorrect labels.

    To solve this, I am looking for a way to rename value labels without having to assign them to the correct variables again.
    I am also open for any other solutions. Thanks a lot in advance!

  • #2
    If you install elabel from SSC, it can do just that.

    Code:
    sysuse auto, clear
    lab list
    elabel rename origin foreign
    lab list
    Res.:

    Code:
    . lab list
    origin:
               0 Domestic
               1 Foreign
    
    . 
    . elabel rename origin foreign
    
    . 
    . lab list
    foreign:
               0 Domestic
               1 Foreign

    Comment


    • #3
      Thanks a lot Andrew, that was exactly what I needed!

      Comment


      • #4
        You can also try to use the ad hoc program -rename_to_match_label_name- as given here (https://www.statalist.org/forums/for...53#post1752953) by daniel klein in discussing a problem of the SSC programs -valtovar- and -renlabv-.

        After you did run the commands given by Daniel to define the program -rename_to_mach_label_name- and assuming that the SPSS files data_01.sav and data_02.sav exist, you can use the following to achieve what you want (note that here I am reading the variable names as lower case and I am appending the data because I don't know what kind of merging you want to use):
        Code:
        tempfile data
        import spss using "data_01.sav", clear case(lower)
        rename_to_match_label_name _all
        save `data', replace
         
        import spss using "data_02.sav", clear case(lower)
        rename_to_match_label_name _all
        append using `data'

        Comment

        Working...
        X