SAS:折叠和加权平均值计算

时间:2016-08-29 05:52:45

标签: sas collapse weighted-average

我有一个SAS编程问题,我无法自行解决,而且我感谢任何影响。

我希望按变量折叠数据集中的数据,并根据另一个变量给出的权重对两个变量进行汇总/平均,并将它们相互减去:

示例数据

number   flag     volume   measure1  measure2
1         A         1         2         2        
2         B         2         4         5
3         A         5         8         20
4         B         10        4         1
5         A         9         10        11
6         B         5         2         9
7         A         4         11        23
8         B         3         1         8

现在:我想要测量1和2的体积加权平均值,然后计算measure1-measure2。然后所有这些按标志A和B分组:

Number Flag      Volume       VolWeightMeasure1      VolWeightMeasure2      FinalMeasure
1        A        19        ((1/19)*2)+((5/19)*8)+...     ...            (VolWeightMeasure1-VolWeightMeasure2)
2        B        20        ((2/20)*5)+((10/20)*1)+...    ...            (VolWeightMeasure1-VolWeightMeasure2)

所以基本上是崩溃但是采用了体积加权测量,然后扣除了两者。 感谢您的任何意见!

最佳

3 个答案:

答案 0 :(得分:0)

这可以使用两个嵌套的datastep语句在单个SET中完成(通常称为双重Do-Loop-of-Whitlock)。

第一个循环聚合VOLUME的值。 在第二个循环中,计算公式。 每组只有一个值进入输出。

data have;
input  flag $ volume measure1 measure2;
datalines;
        A         1         2         2        
        B         2         4         5
        A         5         8         20
        B         10        4         1
        A         9         10        11
        B         5         2         9
        A         4         11        23
        B         3         1         8
run;

proc sort data = have; by flag; run;
data want;

  do _n_ = 1 by 1 until (last.flag);
    set have;
    by flag;

    sum_vol = sum(sum_vol,volume);
  end;

  do _n_ = 1 by 1 until (last.flag);
    set have;
    by flag;

    VolWeightMeasure1 = sum(VolWeightMeasure1,(volume/sum_vol)*measure1);
    VolWeightMeasure2 = sum(VolWeightMeasure2,(volume/sum_vol)*measure2);
  end;

  FinalMeasure = VolWeightMeasure1 - VolWeightMeasure2;  

drop volume measure1 measure2;
rename sum_vol = Volume;
run;

答案 1 :(得分:0)

proc sql;
   select flag,sum_volume,sum1/sum_volume as volweightmeasure1,sum2/sum_volume as volweightmeasure2,
          calculated volweightmeasure1-calculated volweightmeasure2 as finalmeasure
   from (select flag,sum(volume) as sum_volume, sum(volume*measure1) as sum1, sum(volume*measure2) as sum2 from  have group by flag);
quit;

答案 2 :(得分:0)

如果你对proc摘要感到满意/意味着你可以用它完成大部分的工作:

proc summary data=have nway;
  class flag;
  var measure1 measure2;
  wgt volume;
  output out=wantcomp(drop=_:) sumwgt=Volume mean=VolWeightMeasure1 VolWeightMeasure2;
run;

data want;
  set want;
  FinalMeasure = VolWeightMeasure1-VolWeightMeasure2;
run;