s***r 发帖数: 1121 | 1 数据的样子如下:
NameID Year Indicator
1 1988 1
1 1989 1
1 1990 0
2 1985 0
2 1986 1
2 1987 0
2 1988 1
2 1989 0
2 1990 1
2 1991 1
... ....
我想算出对于每一个NAMEID, 在每一年(包括所有以前的年份),有多少的INDICATOR
是1?
例如,对于NameID ”1“ 来说,在1989年和1989年之前,它总共有2个INDICATOR是”1
“。 同样,在1990 年,它仍然有2个INDICATOR是”1“。 依此类推。 有点象moving
average, 只不过更象moving sum.
谢谢。 |
a*****3 发帖数: 601 | 2 i'd use proc freq or Tabulate, hahaha. I know i'm lazy a.
btw 等我弄完手里的活 晚上来写code 拜托大家不要和我抢包子啊。
bow// |
k*******a 发帖数: 772 | 3 这个行吗
data a;
input nameid year indicator;
datalines;
1 1988 1
1 1989 1
1 1990 0
2 1985 0
2 1986 1
2 1987 0
2 1988 1
2 1989 0
2 1990 1
2 1991 1
;
data a;
set a;
by nameid year;
if first.nameid then sum=0;
sum+indicator;
proc print;run; |
l****u 发帖数: 529 | 4 proc sql;
select sum(indicator)
from a
group by nameid,year;
quit; |
s******r 发帖数: 1524 | 5 都抢上了,只好不客气了。
data aaa;
input nameid year indicator;
datalines;
1 1988 1
1 1989 1
1 1990 0
2 1985 0
2 1986 1
2 1987 0
2 1988 1
2 1989 0
2 1990 1
2 1991 1
;
run;
proc sql;
select a.nameid, a.year, sum(b.indicator)
from aaa a inner join aaa b
on a.nameid=b.nameid and a.year>=b.year
group by a.nameid,a.year;quit;run;
【在 a*****3 的大作中提到】 : i'd use proc freq or Tabulate, hahaha. I know i'm lazy a. : btw 等我弄完手里的活 晚上来写code 拜托大家不要和我抢包子啊。 : bow//
|
l***a 发帖数: 12410 | 6 did you try your code?
【在 l****u 的大作中提到】 : proc sql; : select sum(indicator) : from a : group by nameid,year; : quit;
|
s***r 发帖数: 1121 | 7 8个包子sent.
【在 k*******a 的大作中提到】 : 这个行吗 : data a; : input nameid year indicator; : datalines; : 1 1988 1 : 1 1989 1 : 1 1990 0 : 2 1985 0 : 2 1986 1 : 2 1987 0
|
s***r 发帖数: 1121 | 8 3 个包子 for the rest ID who wrote the code, no matter it is correct or not. |
o****o 发帖数: 8077 | 9 data test;
input NameID Year Indicator ;
datalines;
1 1988 1
1 1989 1
1 1990 0
2 1985 0
2 1986 1
2 1987 0
2 1988 1
2 1989 0
2 1990 1
2 1991 1
;
run;
proc expand data=test out=test;
by NameID;
convert indicator=is/transformout=(cusum);
run;
baozi pls. 3ks
【在 s***r 的大作中提到】 : 数据的样子如下: : NameID Year Indicator : 1 1988 1 : 1 1989 1 : 1 1990 0 : 2 1985 0 : 2 1986 1 : 2 1987 0 : 2 1988 1 : 2 1989 0
|
l****u 发帖数: 529 | 10 惭愧,这个包子拿的。
我不但没有试一下,连lz的意图我都理解错了。
【在 l***a 的大作中提到】 : did you try your code?
|