Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: keeping variable labels after -collapse-

From   annoporci <>
To   "" <>
Subject   st: keeping variable labels after -collapse-
Date   Sun, 06 Jan 2013 05:02:11 +0800

Dear Statalist

Nick Cox has a trick to keep variable labels after -collapse-


I found it tedious to copy the pieces of code everytime I needed to
-collapse- so I implemented Nick's algorithm by saving the pieces of code
in separate files and -include- ing them into the master code.

In practice, you only need to remember to "squeeze" -collapse between
"include copylabels" and "include attachlabels".

My contribution is modest, but I send it to Statalist for the record.

All credit goes to Nick Cox. Thanks.

Like so:

     sysuse sp500,clear

     sort date
     tsset date
     gen qdate = qofd(date)
     format qdate %tq
     local variables open close

     cd "C:\stata\labels\"

     include copylabels
     collapse (mean) `variables', by(qdate)
     include attachlabels

     list in 1/4

where I have saved and in the
"C:\stata\labels\" directory.
foreach v of var * {
        label var `v' "`l`v''"
foreach v of var * {
        local l`v' : variable label `v'
            if `"`l`v''"' == "" {
            local l`v' "`v'"

Patrick Toche.
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index