如何计算加权平均值但使用SAS排除对象本身

how to calculate weighted average but exclude the object itself using SAS

我的数据集中有四个变量。 Company 显示公司名称。 Return 是第 DateCompany 的 return。 Weight 是这家公司在市场上的权重。

我想保留原始文件中的所有变量,并创建一个额外的变量,即市场 return(不包括 Company 本身)。股票'a'对应的市场return是市场中除股票a外所有股票return在同一Date的总和。例如,如果市场上有 a、b、c 3 只股票。股票 a 的市场 Return 是 Return(b)* [权重(b)/(权重(b)+权重(C))] + Return(C)* [权重( C)/(weight(b)+weight(C)]。类似地,股票 b 的市场 Return 是 Return(a)* [Weight(a)/(weight(a)+weight( C))] + Return(C)* [权重(C)/(权重(a)+权重(C)].

我尝试使用 proc summary 但是这个函数在计算股票 a 的市场 return 时不能排除股票 a。

PROC SUMMARY NWAY DATA  ; 
    CLASS Date ; 

    VAR Return / WEIGHT = weight;

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

谁能教我如何解决这个问题。我对这个软件比较陌生,所以我不知道我是否应该使用循环或者可能有更好的选择。

这可以通过一些花哨的代数来完成。不过,它不是内置的东西。

基本上:

  • 构建一个"total"市场return
  • 按股票构建股票 return(所以只有 A 的 return)
  • 减去A占总数的部分。

多亏了生成这些列表的简单数学运算,这很容易做到。

Total sum = ((mean of A*Awgt) + (mean of remainder*sum of their weights))/(sum of Awgt + sum of rest wgts)

因此,求解(剩余均值*剩余 wgts 均值/剩余 wgts 之和)。

Exclusive sum: ((mean of all * sum of all wgts) - (mean of A * sum of A wgts)) / (sum of all wgts - sum of A wgts)

像这样。

data returns;
  input stock $ return weight;
  datalines;
A .50 1
B .75 2
C .33 1
;;;;
run;

proc means data=returns;
  class stock;
  types () stock; *this is the default;
  weight weight;
  output out=means_out mean= sumwgt= /autoname;
run;

data returns_excl;
  if _n_=1 then set means_out(where=(_type_=0) rename=(return_mean=tot_return return_sumwgt=tot_wgts));
  set means_out(where=(_type_=1));
  return_excl = (tot_return*tot_wgts-return_mean*return_sumwgt)/(tot_wgts-return_sumwgt);
run;