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]

RE: st: RE: Calculate variances of subsamples

From   "Martin Weiss" <>
To   <>
Subject   RE: st: RE: Calculate variances of subsamples
Date   Sat, 5 Jun 2010 17:41:22 +0200


Your problem may turn out to be easily solved with a -reshape-. It is not a
good idea, though, to have "0.00234(exxon)" in a cell of your data, as this
would have to be stored as a string, precluding any further processing of
the number. Did you write it as an illustration, or do you really want the
cell to contain the string?          


-----Original Message-----
[] On Behalf Of Lars Knuth
Sent: Samstag, 5. Juni 2010 17:28
Subject: Re: st: RE: Calculate variances of subsamples

Ok, great, it took some time, but I finally understood Martin`s
code... this is a great way of learning more about STATA.
My next problem is that I have a variable with the variances for 536
-rolling- steps for each of the stocks.
It looks like this:
Variance         stock name
0.00234          exxon
...........          exxon
0.13444          exxon
0.99388          microsoft
...........           microsoft
0.42445          microsoft
0.42444          intel
........              intel
0.23434           intel

What I would like to have is the following:

0.00234(exxon)          0.99388(microsoft)     0.42444(intel)
...........                    ............                     ............
0.13444                    0.42445                     0.23434(intel)

I could do gen varexxon=Variance if stockname=="exxon"
and that for all the stocks. But even if I do so I get variables with
a lot of missings and I can not write the variances horizontally next
to each other.

But they are from the same time (because of -rolling-) and I need them
to be horizontally ordered without the missings.

I hope my problem becomes clear. I guess what I miss is just a small
Thank you in advance for any hint!

2010/6/2 Martin Weiss <>:
> <>
> You could of course issue the -rolling- call with -clear- present, -save-
> the result to a new file and reload your "3105.dta" to start anew for the
> next stock. The datasets thus -saved- could be -append-ed to form one big
> dataset afterwards. -postfile- is also an option, as always.
> BTW, you may be better of with the lag operator "L." for your return
> calculations.
> Martin
> -----Original Message-----
> From:
> [] On Behalf Of Lars Knuth
> Sent: Mittwoch, 2. Juni 2010 20:22
> To: statalist
> Subject: st: Calculate variances of subsamples
> Dear listers,
> I have to say thanks to Martin, the recommendation of rolling was
> great. Unfortunately, I have now a few problems with the
> implementation.
> 1. -rolling- works with the "clear" option, but without it does not
> ("rolling r(Var), window(60) clear: summarize exret" works)
> 2. I need the data to calculate and store the variances for more than
> 1000 stock price returns in the end, so can I somehow keep all the
> data and then perform -rolling- in a loop?
> 3. Is there also an opportunity to perform the return calculation in a
> I am attaching parts of the code I have so far. Any ideas would be of
> great help to me.
> Thanks in advance!
> clear*
> use "C:\...\3105.dta", clear
> gen int time=_n
> * Return calculation
> gen double exret=ex[_n]/ex[_n-1]-1 if _n>1
> gen double msciret=msci[_n]/msci[_n-1]-1 if _n>1
> gen double msftret=msft[_n]/msft[_n-1]-1 if _n>1
> gen double appret=app[_n]/app[_n-1]-1 if _n>1
> gen double geret=ge[_n]/ge[_n-1]-1 if _n>1
> gen double pgret=pg[_n]/pg[_n-1]-1 if _n>1
> gen double jnjret=jnj[_n]/jnj[_n-1]-1 if _n>1
> gen double bpret=bp[_n]/bp[_n-1]-1 if _n>1
> tsset time
> * Rolling
> rolling r(Var), window(60): summarize exret
> rolling r(Var), window(60): summarize msciret
> rolling r(Var), window(60): summarize msftret
> *
> *   For searches and help try:
> *
> *
> *
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

*   For searches and help try:

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