• "Everything should be made as simple as possible, but not simpler." - Anonymous (although often attributed to Albert Einstein)
  • Enter your email address to subscribe.

  • Categories

  • Recent Comments

  • RSS Statalist: the Stata forum

  • RSS Stackoverflow [Stata]

  • Google Analytics Stats

    Period:Last 30 Days
    Total Visits:11327

Converting time series with -tscollap-


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.

5 Responses

  1. Because in the days of 8.3 DOS command names could be 8 char max. E.g. wntestq

  2. How to download tscollap?, I couldn’t download it. When I try to install by typing ssc instal tscollap, stata mentioned this is not a stata download site

    • hi djoko. it works for me. try to update your Stata (-update all-). if that doesn’t work you may contact Stata’s technical service (tech-support@stata.com). =D

  3. Thanks Mitch!

Leave a Reply

%d bloggers like this: