x****i 发帖数: 400 | 1 data 如下,
ID seq unit
100 1 10
100 2 10
101 1 10
101 1 20
103 1 10
103 2 10
104 1 10
104 1 20
现在要保留每一个ID unique的seq。101 和 104都有两个相同的seq,这样要add unit
together。
ID seq unit
100 1 10
100 2 10
101 1 30
103 1 10
103 2 10
104 1 30
proc sql里面有没有conditional sum的办法呢?尝试了半天没有弄出来。还有其他简
洁的办法吗? |
m*******g 发帖数: 3044 | 2 ?? group, sum should be working |
w****a 发帖数: 114 | 3 proc sql;
create s_data as select id,seq,sum(unit) as s_unit
from data group by id,seq order by id,seq;
quit; |
k*****u 发帖数: 1688 | 4 options formdlim='-';
data test;
input ID seq unit;
cards;
100 1 10
100 2 10
101 1 10
101 1 20
103 1 10
103 2 10
104 1 10
104 1 20
;
run;
proc sort data=test;
by id seq;
run;
data want (keep=id seq sumunit);
set test;
by id seq;
if first.id or first.seq then sumunit=unit;
else sumunit+unit;
if last.id or last.seq then output;
run;
proc print data=want;
run; |
w*****8 发帖数: 395 | 5 data test;
input ID seq unit;
fakeID = id||'_'||seq;
cards;
100 1 10
100 2 10
101 1 10
101 1 20
103 1 10
103 2 10
104 1 10
104 1 20
;
run;
proc sort data=test;
by fakeID;
run;
data want (keep=id seq sumunit);
set test;
by fakeID;
if first.fakeID then sumunit=unit;
else sumunit+unit;
if last.id;
run;
proc print data=want;
run;
【在 k*****u 的大作中提到】 : options formdlim='-'; : data test; : input ID seq unit; : cards; : 100 1 10 : 100 2 10 : 101 1 10 : 101 1 20 : 103 1 10 : 103 2 10
|
w*****8 发帖数: 395 | 6 ....
data want (keep=id seq sumunit);
set test;
by fakeID;
if first.fakeID then sumunit=unit;
else sumunit+unit;
if last.id; <-- should be " if last.fakeID; "
run;
...
【在 w*****8 的大作中提到】 : data test; : input ID seq unit; : fakeID = id||'_'||seq; : cards; : 100 1 10 : 100 2 10 : 101 1 10 : 101 1 20 : 103 1 10 : 103 2 10
|
w*****8 发帖数: 395 | 7 data test;
input ID seq unit;
cards;
100 1 10
100 2 10
101 1 10
101 1 20
103 1 10
103 2 10
104 1 10
104 1 20
;
run;
proc sort;by id seq;run;
proc means;
var unit;
by id seq;
output out=testSum sum=sumUnit;
run;
proc print data=testSum;run;
【在 w*****8 的大作中提到】 : .... : data want (keep=id seq sumunit); : set test; : by fakeID; : if first.fakeID then sumunit=unit; : else sumunit+unit; : if last.id; <-- should be " if last.fakeID; " : run; : ...
|
x****i 发帖数: 400 | 8 4th floor is more general. If there are other variables with different
values, proc sql can not summarize. |