03-02-2012 08:50 AM
Dear Sas members,
I have two stocks per portfolio which I randomly selected.Each stock stays three days in the portfolio and have returns per day. Then the stocks are sold and are invested in the new portfolio.
So I have trouble in porgramming the following;
1) for every portfolio I have to calculate the cumulative return per stock (ok, works for me)
2) At the end of the portfolio (day 3) the stocks are sold and I have to sum the cumulative returns, divide it by two and reinvest it equally in the next two stocks of portfolio 2, and so on.
I have difficulties implementing step 2...
I have uploaded sample data in the attachement;
this is my code for step 1
procsort data=WORK.QUERY_FOR_RETURNS out
PORT STOCK DATE;
*Calculating the retunr per stock in the portfolio;if first.stock then do
;if ret=. then cumret=1
;else if ret NE . then do
03-02-2012 01:55 PM
I am not 100% sure about what you asked for. Here just to start discussion, if you are after the initial investment for each company:
data want (drop=_: cumret);
do until (last.port);
by port stock date;
if first.port then do;
if first.stock then
if last.stock then _c+cumret;
03-03-2012 09:30 AM
But i should have the cumulative return of each stock in portfolio 1. After portfolio 1 the cumulative return of the stocks should be taken (summed/2) and invested against the returns of the stocks of portfolio 2 and so on.
03-03-2012 10:39 AM
I am still not sure I have completely understand what you want. I thought my solution could do what you just asked. if you could just post several line of data to show what your wanted table look like, that would definitely help.