Sas 计算多列百分比
考虑以下SAS表:Sas 计算多列百分比,sas,calculated-columns,Sas,Calculated Columns,考虑以下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
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
我如何才能做到这一点?使用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;
使用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;
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;
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;
到目前为止你试过什么?这不是一个请求站点上的代码,但如果您在某个特定方法上遇到问题,我相信人们可以提供建议。到目前为止,您尝试了什么?这不是一个请求站点上的代码,但如果您在特定方法上遇到问题,我相信人们可以提供建议。