由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - sas question
相关主题
请教一sas programmmsas lag1() 古怪现象请教(小包子)
急问一个SAS 的常见问题sas code求助
overall mean in sas for several variables问个sql问题
sas question面试
SAS helphelp:data manipulation
SAS code 问题sas新手请教一个问题
急!一个简单的SAS问题,请大家帮帮解释一下!多谢!请教flag问题
再请教一个sas问题SAS问题请教
相关话题的讨论汇总
话题: variable话题: sas话题: data话题: run话题: summed
进入Statistics版参与讨论
1 (共1页)
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 的大作中提到】
: 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
3
sql很方便的
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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 你可以先按照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 的大作中提到】
: 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 的大作中提到】
: even simpler :)
: data test0;
: set test;
: if b>=lag1(b) then c=a;
: else c+a;
: run;

l***a
发帖数: 12410
12
你一点sas都不会?

【在 j*******i 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: data a;
: input A B;
: datalines;
: 1 2
: 4 1
: 2 3
: 1 2
: 3 1
: 1 2
: ;

1 (共1页)
进入Statistics版参与讨论
相关主题
SAS问题请教SAS help
SAS Macro 问题请教 。。。SAS code 问题
SAS快捷键问题急!一个简单的SAS问题,请大家帮帮解释一下!多谢!
请教个SAS问题再请教一个sas问题
请教一sas programmmsas lag1() 古怪现象请教(小包子)
急问一个SAS 的常见问题sas code求助
overall mean in sas for several variables问个sql问题
sas question面试
相关话题的讨论汇总
话题: variable话题: sas话题: data话题: run话题: summed