Posted on 12 July 2012 by Mitch Abdon
Kit Baum’s -tscollap- is another gem from the unsecured treasure vault of user-written Stata commands. It compacts timeseries (and panel) data, creating a new dataset of means, sums, end-of-period values, and more (see -help tscollap-). For example, you have a monthly trade data that you want to add up to generate annual data or monthly data on broad money from which you only want to pick end-of-year observation.
Why not just use -collapse-, a built-in command that also converts data into means, sums, etc.? Sure, you may use -collapse-. But for timeseries data -tscollap- offers convenience. For one, a new tsset variable freq_freq (e.g., q_q, y_y, or whatever you have indicated in the generate option if you did) is generated, i.e., you need not -tsset- or -xtset- your data again before using time series commands or functions.
Let us compare -collapse- and -tscollap- below using the quarterly macro data lutkepohl2.dta from Stata website. First, we use -collapse- to calculate annual data for investment, income, and consumption. To use -collapse-, we need a grouping variable—in this case a variable that indicates year—over which the sum will be calculated.
webuse lutkepohl2, clear // load data from Stata website
gen year = year(dofq(qtr)) // extract the year component from variable qtr
collapse (sum) inv inc consump, by(year)
Notice that variable year is not read as a time variable (type -tsset- to display the current tsset settings). Thus, we must -tsset- the data again before we can use functions and commands for timeseries analysis. The following illustrates the use of -tscollap-:
webuse lutkepohl2, clear
tscollap (sum) inv inc consump, to(y)
-tscollap- gives the same annual data but with the bonus of automatically generating a new time variable. In the example above, variable y_y is the time variable.
-tscollap- is available from SSC. Type -ssc install tscollap- to install.
I have one question though. Why was it named -tscollap- and not -tscollapse-? Brevity? Just curious.