Bookmark and Share

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


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

st: keeping variable labels after -collapse-


From   annoporci <[email protected]>
To   "[email protected]" <[email protected]>
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-

Reference:

http://www.stata.com/support/faqs/data-management/keeping-same-variable-with-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 attachlabels.do and copylabels.do in the
"C:\stata\labels\" directory.

attachlabels.do
///
foreach v of var * {
        label var `v' "`l`v''"
}
///


copylabels.do
///
foreach v of var * {
        local l`v' : variable label `v'
            if `"`l`v''"' == "" {
            local l`v' "`v'"
        }
}
///


--
Patrick Toche.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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