[英]calculate market weighted return using SAS
I have four variables Name
, Date
, MarketCap
and Return
. 我有四个变量
Name
, Date
, MarketCap
和Return
。 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.