1
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報告,產品中斷後給出了每個產品的總數量。如何計算右側的額外列來計算基於小計的產品數量百分比?PROC報告中的計算百分比
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報告,產品中斷後給出了每個產品的總數量。如何計算右側的額外列來計算基於小計的產品數量百分比?PROC報告中的計算百分比
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;