考虑以下SAS表:
data input;
input Row$ Col1 Col2;
datalines;
A 10 20
B 20 20
C 20 20
;
run;
我想计算另外两列 - “Col1_col_pct”和“Col2_col_pct”,它们分别是列“Col1”和“Col2”的列百分比,它们将产生以下输出表:
Row Col1 Col2 Col1_col_pct Col2_col_pct
A 10 20 20.00 33.33
B 20 20 40.00 33.33
C 20 20 40.00 33.33
我该怎么做?
答案 0 :(得分:2)
使用SUMMARY计算每列的总和,然后将其重新连接到原始列。然后计算你的百分比。
data input;
input Row$ Col1 Col2;
datalines;
A 10 20
B 20 20
C 20 20
;
run;
proc summary data=input;
var col1 col2;
output out=sum(drop=_type_ _freq_) sum= /autoname;
run;
data want(drop=col1_sum col2_sum);
set input;
if _n_ = 1 then
set sum;
Col1_pct = col1 / col1_sum;
col2_pct = col2 / col2_sum;
run;
答案 1 :(得分:2)
PROC SQL也可以这样做,虽然它仍然是手册:
Proc sql;
create table want as
select *,
col1/sum(col1) as PCT_COL1 format=percent8.2,
col2/sum(col2) as PCT_COL2 format=percent8.2
from input;
quit;