简体   繁体   English

使用SAS计算市场加权回报

[英]calculate market weighted return using SAS

I have four variables Name , Date , MarketCap and Return . 我有四个变量NameDateMarketCapReturn Name is the company name. Name是公司名称。 Date is the time stamp. Date是时间戳。 MarketCap shows the size of the company. MarketCap显示了公司的规模。 Return is its return at day Date . Return是它在Date返回。

I want to create an additional variable MarketReturn which is the value weighted return of the market at each point time. 我想创建一个额外的变量MarketReturn ,它是每个时间点市场的价值加权回报。 For each day t, MarketCap weighted return = sum [ return(i)* (MarketCap(i)/Total(MarketCap) ] (return(i) is company i's return at day t). 对于每天t,MarketCap加权回报=总和[return(i)*(MarketCap(i)/ Total(MarketCap)](return(i)是公司i在第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. 我想必须有一些功能可以很容易地在SAS中实现这个目标,所以我想问一下是否有人可以改进我的代码。

step1: sort data by date step2: calculate total market value at each day TotalMV = sum(MarketCap). 步骤1:按date排序数据步骤2:计算每天的总市场价值TotalMV = sum( TotalMV )。 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. 步骤3:计算权重为每个公司(重量= MarketCap / TotalMV )步骤4:创建一个新的变量“贡献” =返回*重量为每个公司STEP5:总结Contribution在每一天。 Sum(Contribution) 总和(贡献)

Weighted averages are supported in a number of SAS PROCs. 许多SAS PROC都支持加权平均值。 One of the more common, all-around useful ones is PROC SUMMARY : 其中一个更常见,全面有用的是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. NWAY部分告诉PROC,观察结果应仅按CLASS声明中的规定进行分组 - 它不应提供未分组的总数等。

The CLASS Date piece tells the PROC to group the observations by date. CLASS Date片段告诉PROC按日期对观察结果进行分组。 You do not need to pre-sort the data when you use CLASS . 使用CLASS时,无需对数据进行预排序。 You do have to pre-sort if you say BY Date instead. 如果你说BY Date你必须预先排序。 The only rationale for using BY is if your dataset is very large and naturally ordered, you can gain some performance. 使用BY的唯一理由是,如果您的数据集非常大且自然排序,您可以获得一些性能。 Stick to CLASS in most cases. 在大多数情况下坚持CLASS

VAR Return / WEIGHT = MarketCap tells the proc that any weighted calculations on Return should use MarketCap as the weight. VAR Return / WEIGHT = MarketCap告诉proc,对Return任何加权计算都应使用MarketCap作为权重。

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 . 最后, OUTPUT语句指定要将结果写入的数据集(使用OUT选项),并指定将Return的平均值计算为MarketReturn

There are many, many more things you can do with PROC SUMMARY . 使用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. PROC SUMMARY的文档很少,但仅仅因为它与PROC MEANS几乎完全相同,并且SAS不希望为两者生成大部分相同文档的大量内容。 Here is the link to the SAS 9.4 PROC MEANS documentation . 以下是SAS 9.4 PROC MEANS文档的链接 The main difference between the two PROCS is that SUMMARY only outputs to a dataset, while MEANS by default outputs to the screen. 两个PROCS之间的主要区别是, SUMMARY只输出到数据集,而MEANS默认输出到屏幕上。 Try PROC MEANS if you want to see the result pop up on the screen right away. 如果您想立即在屏幕上看到结果,请尝试PROC MEANS

The MEAN keyword in the OUTPUT statement comes from SAS's list of statistical keywords, a helpful reference for which is here . OUTPUT语句中的MEAN关键字来自SAS的统计关键字列表, 这里有一个有用的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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