I have four variables Name
, Date
, MarketCap
and Return
. Name
is the company name. Date
is the time stamp. MarketCap
shows the size of the company. Return
is its return at day Date
.
I want to create an additional variable MarketReturn
which is the value weighted return of the market at each point time. For each day t, MarketCap weighted return = sum [ return(i)* (MarketCap(i)/Total(MarketCap) ] (return(i) is company i's return at day t).
The way I do this is very inefficient. I guess there must be some function can easily achieve this traget in SAS, So I want to ask if anyone can improve my code please.
step1: sort data by date
step2: calculate total market value at each day TotalMV
= sum(MarketCap).
step3: calculate the weight for each company (weight = MarketCap
/TotalMV
)
step4: create a new variable 'Contribution' = Return * weight for each company
step5: sum up Contribution
at each day. Sum(Contribution)