楼主: julius725
7491 18

[原创博文] 累加问题,拜求SAS达人 [推广有奖]

11
fanly111 发表于 2008-10-17 19:16:00
data production;
input production@@;
cards;
100 105 110 115 120 125 130 135 140 145 150 155 160
165 170 175 180 185 190 195 200 205 210 215 220 225
230 235 240 245 250 255 260 265 270 275 280 285
;
run;
proc print data=production;
sum production;
run;

12
宜桦 发表于 2008-10-18 03:56:00

Here is the modified code and I also put comments there for your easier reading. I am afraid you are not only doing the cummalative calculation for all of the observations of one variable. If you need to calculate the cumm for each category of this variable, you have to put this variable before year in all the 'by' statements and change if _n_ = 1 to if first.category_variable_name

Hope it helps.

/*create year var */

data yr;

do year = 1970 to 2007;

    output;

end;

run;

/* create production var */

data prod;

input production@@;

cards;

100 105 110 115 120 125 130 135 140 145 150 155 160

165 170 175 180 185 190 195 200 205 210 215 220 225

230 235 240 245 250 255 260 265 270 275 280 285

;

run;

100 105 110 115 120 125 130 135 140 145 150 155 160

165 170 175 180 185 190 195 200 205 210 215 220 225

230 235 240 245 250 255 260 265 270 275 280 285

;

run;

/* make up a fake dataset for the calculation */

data com;

merge yr prod;

run;

/* the following is the part you really want */

proc sort data = com; by year; run;

data com;

set com;

by year;

if _n_ = 1 then cumm = 0;

retain cumm;

cumm = sum (production, cumm);

run;

cumm = sum (production, cumm);

run;

I really have troble to post here.

[此贴子已经被作者于2008-10-18 4:10:26编辑过]

13
宜桦 发表于 2008-10-18 04:12:00

Here is the modified code and I also put comments there for your easier reading. I am afraid you are not only doing the cummalative calculation for all of the observations of one variable. If you need to calculate the cumm for each category of this variable, you have to put this variable before year in all the 'by' statements and change if _n_ = 1 to if first.category_variable_name

Hope it helps.

/*create year var */

 data yr;

 do year = 1970 to 2007;

     output;

 end;

 run;

 /* create production var */

 data prod;

 input production@@;

 cards;

 100 105 110 115 120 125 130 135 140 145 150 155 160

 165 170 175 180 185 190 195 200 205 210 215 220 225

 230 235 240 245 250 255 260 265 270 275 280 285

 ;

run;

 /* make up a fake dataset for the calculation */

 data com;

 merge yr prod;

 run;

 /* the following is the part you really want */

 proc sort data = com; by year; run;

 data com;

 set com;

 by year;

 if _n_ = 1 then cumm = 0;

 retain cumm;

 cumm = sum (production, cumm);

run;

 

[此贴子已经被作者于2008-10-18 4:13:31编辑过]

14
hotlip 发表于 2008-10-18 07:45:00

好奇怪,难道是我理解错了,后面的程序看着都很强,但有些明明就不对。还用retain,还_n_=1,又没有first..last.什么的。

以下是我的答案,不知是否理解有误。

data out;

    set in;

    y+x;

run;

如果是用sum函数,就要加retain。

如果你要对多列累加,就用y+x1+x2....

你似乎没有其它要求阿。

15
qwqwqw 发表于 2008-10-18 14:54:00

这个问题你早说清楚就好了。

设数据集a中有你的粮食产量x,

data b;
set a;
retain y 0;
y=y+x;
output;
run;

新的数据集b中就有粮食产量x和累加变量y(你所要的)。

16
julius725 发表于 2008-10-19 21:39:00

试过了,用retain语句可行,

retain y 0;

y=y+x;

y就是累加值变量,谢谢楼上达人相助。

小弟是SAS新手,还有一事相问,我把"retain y 0;"一句换为'y=0;‘结果y就完全和x相等不做累加,SAS的retain语句是怎样一个运行流程呢?

17
yvincent 发表于 2008-10-19 23:40:00

1、下面用累加运算来解决你的问题,语句简单一点。

 data prod;
 input production@@;
 cards;
 100 105 110 115 120 125 130 135 140 145 150 155 160
 165 170 175 180 185 190 195 200 205 210 215 220 225
 230 235 240 245 250 255 260 265 270 275 280 285
 ;
run;

data b;
set prod;
y+production;
run;

2、retain y 表示在data步循环的时候y保留当前步的值直到下一步。retain y 0 中的0表示设置的y的初始值。

18
pandasasa 发表于 2008-11-10 12:53:00

可以用retain语句增添两个变量 year 和y(所谓在变的变量)

data a;

input x@@;

retain year 1969 y 0;/*其中1969代表年的初始值,0代表y的初始值*/

year=year+1;

y=y+x;

cards;

(30年的数据)

run;

19
hbwzhsh 发表于 2010-5-14 11:17:30
在这里见识了
不懂就要问

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-26 20:59