a**w 发帖数: 60 | 1 proc sql里可以实现, 但是code 显得太笨:
proc sql;
create table a as
select *,
sum(P1) as sum_P1,
sum(P2) as sum_P2,
sum(P3) as sum_P3,
sum(P4) as sum_P4,
.
.
.
sum(P98) as sum_98,
sum(P99) as sum_99,
sum(P100) as sum_100,
sum(Q) as sum_Q,
calculated sum_P1/calculated sum_Q as F1,
calculated sum_P2/calculated sum_Q as F2,
calculated sum_P3/calculated sum_Q as F3,
calculated sum_P4/calculated sum_Q as F4,
.
.
.
calculated sum_P98/calculated sum_Q as F98,
calculated sum_P99/calculated sum_Q as F99,
calculated sum_P1oo/calculated sum_Q as F100
from s
group by var1, var2, var3
order by var1, var2, var3;
quit; | a**w 发帖数: 60 | 2 I tried to use a data step, but can't get the requested result.
%macro sum;
data a1;
set s;
by var1 var2 var3;
%do i=1 %to 100;
if first.var3 then %do;
sum_QH&i.=0;
sum_QH&i.+QH&i.;
sum_Q=0;
SUM_Q+Q;
if last.var3;
%end;
%end;
run;
data a;
set a1;
%do %i=1 %to 100;
F=sum_QH&i./sum_Q;
%end;
run;
%mend sum;
%sum | m******r 发帖数: 1033 | 3 你这不是写的挺好么。 基本就是这个思路。 用first. , last. 进行累加,计数.
【在 a**w 的大作中提到】 : I tried to use a data step, but can't get the requested result. : %macro sum; : data a1; : set s; : by var1 var2 var3; : %do i=1 %to 100; : if first.var3 then %do; : sum_QH&i.=0; : sum_QH&i.+QH&i.; : sum_Q=0;
| a**w 发帖数: 60 | 4 magliner, thank you for the encouragement (sorry can't type Chinese at the
company).
But the code can't produce the same result as the one from the PROC SQL. So
my question is:
In the data step, can we exactly summarize based on a few sorted "by
variables"?
As in the first. and last., we can only include one variable rather than all
"by variables". The result therefore I got is something unexpected.
【在 m******r 的大作中提到】 : 你这不是写的挺好么。 基本就是这个思路。 用first. , last. 进行累加,计数.
| a**w 发帖数: 60 | 5 问题解决了. 用了一个宏loop, proc sql 和 data step merge.
但是data step的方法, 还是不清楚. 可能data step实现不了
【在 m******r 的大作中提到】 : 你这不是写的挺好么。 基本就是这个思路。 用first. , last. 进行累加,计数.
| m******r 发帖数: 1033 | 6 你试两个变量, 不用红,纯数据步, 纯累加, 看和sql得到的结果一不一样。
就这么点东西,还loop一百次 过一星期自己都看不明白了。
【在 a**w 的大作中提到】 : 问题解决了. 用了一个宏loop, proc sql 和 data step merge. : 但是data step的方法, 还是不清楚. 可能data step实现不了
| a**w 发帖数: 60 | 7 我被do loop搞昏了. 经你提醒, 才发现几个变量在sort之后累积的结果, 跟sql得到的
值是一样的.
【在 m******r 的大作中提到】 : 你试两个变量, 不用红,纯数据步, 纯累加, 看和sql得到的结果一不一样。 : 就这么点东西,还loop一百次 过一星期自己都看不明白了。
| c*******7 发帖数: 2506 | |
|