简体   繁体   中英

calculate market weighted return using SAS

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)

Weighted averages are supported in a number of SAS PROCs. One of the more common, all-around useful ones is PROC SUMMARY :

PROC SUMMARY NWAY DATA = my_data_set ; 
    CLASS Date ; 

    VAR Return / WEIGHT = MarketCap ;

    OUTPUT
       OUT = my_result_set 
       MEAN (Return) = MarketReturn
    ;
RUN;

The NWAY piece tells the PROC that the observations should be grouped only by what is stated in the CLASS statement - it shouldn't also provide an ungrouped grand total, etc.

The CLASS Date piece tells the PROC to group the observations by date. You do not need to pre-sort the data when you use CLASS . You do have to pre-sort if you say BY Date instead. The only rationale for using BY is if your dataset is very large and naturally ordered, you can gain some performance. Stick to CLASS in most cases.

VAR Return / WEIGHT = MarketCap tells the proc that any weighted calculations on Return should use MarketCap as the weight.

Lastly, the OUTPUT statement specifies the data set to write the results to (using the OUT option), and specifies the calculation of a mean on Return that will be written as MarketReturn .

There are many, many more things you can do with PROC SUMMARY . The documentation for PROC SUMMARY is sparse, but only because it is the nearly identical sibling of PROC MEANS , and SAS did not want to produce reams of mostly identical documentation for both. Here is the link to the SAS 9.4 PROC MEANS documentation . The main difference between the two PROCS is that SUMMARY only outputs to a dataset, while MEANS by default outputs to the screen. Try PROC MEANS if you want to see the result pop up on the screen right away.

The MEAN keyword in the OUTPUT statement comes from SAS's list of statistical keywords, a helpful reference for which is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM