SAS问题:求和并除以达到特定值
SAS problem: sum up rows and divide till it reach a specific value
我有以下问题,我想对一列求和,然后将每一行的总和除以整列的总和,直到达到特定值。所以在伪代码中它看起来像这样:
data;
set auto;
sum_of_whole_column = sum(price);
subtotal[i] = 0;
i =1;
do until (subtotal[i] = 70000)
subtotal[i] = (subtotal[i] + subtotal[i+1])/sum_of_whole_column
i = i+1
end;
run;
我得到的错误是我没有定义一个数组...那么我可以使用其他东西而不是小计[i]吗?我怎样才能把一个列放在一个数组中?我试过了,但它不起作用(data = auto and price the column I want to put into an array)
data invent_array;
set auto;
array price_array {1} price;
run;
编辑:也许我使用的数据集有帮助:)
DATA auto ;
LENGTH make $ 20 ;
INPUT make $ 1-17 price mpg rep78 ;
CARDS;
AMC Concord 4099 22 3
AMC Pacer 4749 17 3
Audi 5000 9690 17 5
Audi Fox 6295 23 3
BMW 320i 9735 25 4
Buick Century 4816 20 3
Buick Electra 7827 15 4
Buick LeSabre 5788 18 3
Cad. Eldorado 14500 14 2
Olds Starfire 4195 24 1
Olds Toronado 10371 16 3
Plym. Volare 4060 18 2
Pont. Catalina 5798 18 4
Pont. Firebird 4934 18 1
Pont. Grand Prix 5222 19 3
Pont. Le Mans 4723 19 3
;
RUN;
也许我没有理解你的意思,但如果你除以它的列的总和,你的小计永远不会等于 70 000。最大值将为 1。但是您的增量总和可以等于或大于 70 000。
data stage1;
retain _sum 0;
set auto;
_sum = sum(_sum, price);
if _sum < 70000 then output;
run;
proc sql;
create table want as
select t1.*, t1._sum/sum(price) as subtotal
from stage1 as t1;
quit;
subtotal
0.0607268256
0.1310834235
0.2746411058
0.3679017467
0.5121261056
0.5834753107
0.6994325842
0.7851820027
1
我有以下问题,我想对一列求和,然后将每一行的总和除以整列的总和,直到达到特定值。所以在伪代码中它看起来像这样:
data;
set auto;
sum_of_whole_column = sum(price);
subtotal[i] = 0;
i =1;
do until (subtotal[i] = 70000)
subtotal[i] = (subtotal[i] + subtotal[i+1])/sum_of_whole_column
i = i+1
end;
run;
我得到的错误是我没有定义一个数组...那么我可以使用其他东西而不是小计[i]吗?我怎样才能把一个列放在一个数组中?我试过了,但它不起作用(data = auto and price the column I want to put into an array)
data invent_array;
set auto;
array price_array {1} price;
run;
编辑:也许我使用的数据集有帮助:)
DATA auto ;
LENGTH make $ 20 ;
INPUT make $ 1-17 price mpg rep78 ;
CARDS;
AMC Concord 4099 22 3
AMC Pacer 4749 17 3
Audi 5000 9690 17 5
Audi Fox 6295 23 3
BMW 320i 9735 25 4
Buick Century 4816 20 3
Buick Electra 7827 15 4
Buick LeSabre 5788 18 3
Cad. Eldorado 14500 14 2
Olds Starfire 4195 24 1
Olds Toronado 10371 16 3
Plym. Volare 4060 18 2
Pont. Catalina 5798 18 4
Pont. Firebird 4934 18 1
Pont. Grand Prix 5222 19 3
Pont. Le Mans 4723 19 3
;
RUN;
也许我没有理解你的意思,但如果你除以它的列的总和,你的小计永远不会等于 70 000。最大值将为 1。但是您的增量总和可以等于或大于 70 000。
data stage1;
retain _sum 0;
set auto;
_sum = sum(_sum, price);
if _sum < 70000 then output;
run;
proc sql;
create table want as
select t1.*, t1._sum/sum(price) as subtotal
from stage1 as t1;
quit;
subtotal
0.0607268256
0.1310834235
0.2746411058
0.3679017467
0.5121261056
0.5834753107
0.6994325842
0.7851820027
1