Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Using postfile to save single observations from a dataset


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Using postfile to save single observations from a dataset
Date   Thu, 28 Mar 2013 16:58:30 +0100

On Thu, Mar 28, 2013 at 4:37 PM, Nick Baker wrote:
> I currently have a time series dataset where the variables are the returns
> of several hundred mutual funds:
>
>         Date    retfund1        retfund2
> 1       2006m1  -12.38985       10.39785
> 2       2006m2  -8.219995       9.54027
> 3       2006m3  5.497518        -5.947091
> 4       2006m4  -0.9465399      -2.846269
> 5       2006m5  2.219898        -12.51931
> 6       2006m6  0.4326888       -2.443768
>
> What I want to do is to get these data into a long form with information on
> lagged returns:
>
> FundID  Date    ret(t-1)        ret(t-2)
> 1       2006m1  .       .
> 1       2006m2  -12.38985       .
> 1       2006m3  -8.219995       -12.38985
> 1       2006m4  5.497518        -8.219995
> 1       2006m5  -0.9465399      5.497518
> 1       2006m6  2.219898        -0.9465399
> 2       2006m1  .       .
> 2       2006m2  10.39785        .
> 2       2006m3  9.54027 10.39785
> 2       2006m4  -5.947091       9.54027
> 2       2006m5  -2.846269       -5.947091
> 2       2006m6  -12.51931       -2.846269
>
>
> Currently, for each fund I am generating variables of lagged returns and am
> then attempting to save each observation to a seperate dataset using
> postfile within a forvalues loop over the date range.

That seems inefficient. What about this:

*------------------ begin example ------------------
// create some example data
clear all
input  date   retfund1       retfund2
       552   -12.38985       10.39785
       553    -8.219995       9.54027
       554     5.497518      -5.947091
       555    -0.9465399     -2.846269
       556     2.219898     -12.51931
       557     0.4326888     -2.443768
end
format date %tm

// reshape
reshape long retfund, i(date) j(fund)

// declare as time series data (within fund)
tsset fund date

// create the lagged variables
gen ret1 = L1.retfund
gen ret2 = L2.retfund

// look at the results
list, sepby(fund)
*------------------- end example -------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index