j*******i 发帖数: 97 | 1 how to sum up one variabble according to another variable in SAS?
thanks in advance.
example:
A B
1 2
4 1
2 3
1 2
3 1
1 2
output a new variable
C
1+4=5
2
1+3+1=5 |
l***a 发帖数: 12410 | 2 use retain
【在 j*******i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : how to sum up one variabble according to another variable in SAS? : thanks in advance. : example: : A B : 1 2 : 4 1 : 2 3 : 1 2 : 3 1 : 1 2
|
k*******a 发帖数: 772 | |
j*******i 发帖数: 97 | 4 use retain can sum up one variable for sure.
however, how can I refer to the second variable through the summation
process?
do you have an example?
【在 l***a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : use retain
|
l***a 发帖数: 12410 | 5 data test;
input a b;
cards;
1 2
4 1
2 3
1 2
3 1
1 2
;
run;
data test0;
set test;
retain c;
if (lag1(b)=3 and b in (1,2)) or (lag1(b) in (1,2) and b=3) then c=a;
else c+a;
run;
【在 j*******i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : use retain can sum up one variable for sure. : however, how can I refer to the second variable through the summation : process? : do you have an example?
|
j*******i 发帖数: 97 | 6 Thanks for your quick reply. The code is clean but seems to handle a
different problem.
I guess I need to be more specific on the usage of column b.
the meaning of b is the number of value a to be summed up.
for sample, in your case,
if the first b=2 then the first two rows of a will be summed as c=1+4=5.
the second b=1 then the 3RD row of a will be summed as c=2.
the third b=3 then the 4th,5th,6th rows of a to be summed as c=1+3+1=5.
please let me know if I am not understood.
【在 l***a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : data test; : input a b; : cards; : 1 2 : 4 1 : 2 3 : 1 2 : 3 1 : 1 2 : ;
|
k*******a 发帖数: 772 | 7 你可以先按照b 把a的值group起来,然后在求和
data a;
input A B;
datalines;
1 2
4 1
2 3
1 2
3 1
1 2
;
proc print;run;
data b(keep=group);
set a;
group+1;
do i=1 to B;
output;
end;
proc print data=b;run;
data c;
merge a b;
if A ne .;
run;
proc sql;
select sum(A)
from c
group by group;
quit; |
l***a 发帖数: 12410 | 8 even simpler :)
data test0;
set test;
if b>=lag1(b) then c=a;
else c+a;
run;
【在 j*******i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Thanks for your quick reply. The code is clean but seems to handle a : different problem. : I guess I need to be more specific on the usage of column b. : the meaning of b is the number of value a to be summed up. : for sample, in your case, : if the first b=2 then the first two rows of a will be summed as c=1+4=5. : the second b=1 then the 3RD row of a will be summed as c=2. : the third b=3 then the 4th,5th,6th rows of a to be summed as c=1+3+1=5. : please let me know if I am not understood.
|
j*******i 发帖数: 97 | 9 thanks a lot. this is the natural way.
somehow I feel there might be some tricks to make it a little simplified.
【在 k*******a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 你可以先按照b 把a的值group起来,然后在求和 : data a; : input A B; : datalines; : 1 2 : 4 1 : 2 3 : 1 2 : 3 1 : 1 2
|
s******y 发帖数: 352 | 10 data a;
input A B;
datalines;
1 2
4 1
2 3
1 2
3 1
1 2
;
run;
data b;
set a;
call missing(c);
do _n_=1 to B;
set a (keep=a);
c+a;
end;
run;
BAOZI please!!
【在 j*******i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Thanks for your quick reply. The code is clean but seems to handle a : different problem. : I guess I need to be more specific on the usage of column b. : the meaning of b is the number of value a to be summed up. : for sample, in your case, : if the first b=2 then the first two rows of a will be summed as c=1+4=5. : the second b=1 then the 3RD row of a will be summed as c=2. : the third b=3 then the 4th,5th,6th rows of a to be summed as c=1+3+1=5. : please let me know if I am not understood.
|
j*******i 发帖数: 97 | 11 I am not sure that you got the point.
if you calculate the sum by hand, it just didn't match starting from row 1.
【在 l***a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : even simpler :) : data test0; : set test; : if b>=lag1(b) then c=a; : else c+a; : run;
|
l***a 发帖数: 12410 | 12 你一点sas都不会?
【在 j*******i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I am not sure that you got the point. : if you calculate the sum by hand, it just didn't match starting from row 1.
|
j*******i 发帖数: 97 | 13 this one is cute. I like this one. are you a CS guy? :)
thanks a lot.
【在 s******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : data a; : input A B; : datalines; : 1 2 : 4 1 : 2 3 : 1 2 : 3 1 : 1 2 : ;
|
j*******i 发帖数: 97 | 14 BAOZI sent. :)
thanks again.
【在 s******y 的大作中提到】![](/moin_static193/solenoid/img/up.png) : data a; : input A B; : datalines; : 1 2 : 4 1 : 2 3 : 1 2 : 3 1 : 1 2 : ;
|