计算PROC报告中的百分比

时间:2013-07-18 16:00:45

标签: sas

PRODUCT CODE Quantity
A         1   100
A         2   150
A         3    50
total product A 300
B         1   10
B         2   15
B         3    5
total product B 30

我做了一个proc报告,产品之后的断裂给了我每个产品的总数量。如何计算右侧的额外列以根据小计计算产品的百分比数量?

1 个答案:

答案 0 :(得分:1)

SAS在他们的文档here中有一个很好的例子。我重复了一部分内容,下面还有一些额外的评论。请参阅初始数据集和格式的链接(或自己创建基本数据集)。

proc report data=test nowd split="~" style(header)=[vjust=b];
   format question $myques. answer myyn.;
   column question answer,(n pct cum) all;
   /* Since n/pct/cum are nested under answer, they are columns 2,3,4 and 5,6,7  */
   /* and must be referred to as _c2_ _c3_ etc. rather than by name */
   /* in the OP example this may not be the case, if you have no across nesting */

   define question / group "Question";
   define answer / across "Answer";
   define pct / computed "Column~Percent" f=percent8.2;
   define cum / computed "Cumulative~Column~Percent" f=percent8.2;
   define all / computed "Total number~of answers";

   /* Sum total number of ANSWER=0 and ANSWER=1 */
   /* Here, _c2_ refers to the 2nd column; den0 and den1 store the sums for those. */
   /* compute before would be compute before <variable> if there is a variable to group by */
   compute before;
      den0 = _c2_;
      den1 = _c5_;
   endcomp;

   /* Calculate percentage */
   /* Here you divide the value by its denominator from before */
   compute pct;
      _c3_ = _c2_ / den0;
      _c6_ = _c5_ / den1;
   endcomp;

   /* This produces a summary total */
   compute all;
      all = _c2_ + _c5_;

      /* Calculate cumulative percent */
      temp0 + _c3_;
      _c4_ = temp0;
      temp1 + _c6_;
      _c7_ = temp1;
   endcomp;
run;