Announcement

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

  • Preserving variable labels when I use -collapse-

    Hi all,

    As you know, -collapse- does not preserve the label of variables that it operates on. For example if a variable is named "age" with the label "age of subject", after using

    .collapse (mean) age, by (id)

    the variable age gets a new name "(mean) age" and a label "(mean) age". Is it possible to keep the label when we use -collapse-?

    Thanks,
    Navid

  • #2
    There is no option in -collapse- to retain the original label (though it might be a nice thing to put on the Wish List). But you can store the labels before you -collapse- and then re-apply them. For example:

    Code:
    //    STORE VARIABLE LABELS IN LOCAL MACROS
    foreach v of varlist whatever {
        local `v'_label: var label `v'
    }
    
    collapse (mean) `whatever', by(whatever_else)
    
    //    NOW REAPPLY THE ORIGINAL LABELS
    foreach v of varlist whatever {
        label var `v' `"``v'_label'"'
    }
    Evidently if there is only one variable you are collapsing, as in your example, you don't need to set up loops to manage the storing and re-application of the original labels: just create the one local macro and then issue a single -label var- command.

    Comment

    Working...
    X