楼主: luvtsing
4332 7

如何条件求和 [推广有奖]

  • 10关注
  • 3粉丝

讲师

65%

还不是VIP/贵宾

-

威望
0
论坛币
480 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
655 点
帖子
164
精华
0
在线时间
874 小时
注册时间
2010-5-1
最后登录
2023-5-24

楼主
luvtsing 发表于 2012-12-11 03:42:11 来自手机 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
type  year  sale
A        1        5
B         2        6
A         2         7
B        1          3
我想在右边添加一列为各type每年进展的sale,如下
type  year  sale  cum_sale
A        1        5        5
B         2        6        9
A         2         7        12
B        1          3          3
大大们,请问这代码该如何写呢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:条件求和 sale type year ale 如何

沙发
pingguagain 发表于 2012-12-11 05:58:47
I guess you are asking how to implement it in EXCEL. Here is one way:
Type         Year        Sale        Cum_Sale
A        1        5        =SUMIF($A$1:A2,A2,$C$1:C2)
B        2        6        =SUMIF($A$1:A3,A3,$C$1:C3)
A        2        7        =SUMIF($A$1:A4,A4,$C$1:C4)
B        1        3        =SUMIF($A$1:A5,A5,$C$1:C5)

藤椅
pingguagain 发表于 2012-12-11 06:06:16
pingguagain 发表于 2012-12-11 05:58
I guess you are asking how to implement it in EXCEL. Here is one way:
Type         Year        Sale        Cum_Sale
A        1 ...
It seems that some Year 2 data are ahead of Year 1. Are you allowd to sort the data based on Year first? If not, it seems hard to use sumif here. However, it is easy to implement it using VBA to write a few lines of code.

板凳
pingguagain 发表于 2012-12-11 06:15:42
My bad. You can do it using sumifs as follows:
Type         Year        Sale        Cum_Sale
A        1        5        =SUMIFS($C$2:$C$5,$A$2:$A$5,A2,$B$2:$B$5, "<="&B2)
B        2        6        =SUMIFS($C$2:$C$5,$A$2:$A$5,A3,$B$2:$B$5, "<="&B3)
A        2        7        =SUMIFS($C$2:$C$5,$A$2:$A$5,A4,$B$2:$B$5, "<="&B4)
B        1        3        =SUMIFS($C$2:$C$5,$A$2:$A$5,A5,$B$2:$B$5, "<="&B5)

报纸
luvtsing 发表于 2012-12-11 08:12:16
pingguagain 发表于 2012-12-11 06:15
My bad. You can do it using sumifs as follows:
Type         Year        Sale        Cum_Sale
A        1        5        =SUMIFS($C$2:$C$5,$A ...
哥,我用的是SAS,不是Excel。而且我的表是很大的,没可能一个个写吧

地板
pobel 在职认证  发表于 2012-12-11 09:21:23
data have;
input type $  year  sale;
cards;
A        1        5
B         2        6
A         2         7
B        1          3
;

data wanted;
  if 0 then set have;
   *** define Hash object;
   declare hash h(dataset:'have' ,ordered: 'Y');
   declare hiter hi('h');
        h.definekey('type','year');
                h.definedata('type','year','sale');
                h.definedone();

   *** calculate cumulative sale;
        rc=hi.first();
           _type=type; _sale=sale;
       do while(rc=0);
              rc=hi.next();
                  if type=_type and rc=0 then do; sale=sum(sale,_sale);  h.replace(); end;
                  _type=type; _sale=sale;
                end;

   *** add the cum_sale to original dataset;
   do until(done);
      set have(rename=(sale=orig_sale)) end=done;
          h.find();
          output;
          rename orig_sale=sale sale=cum_sale;
          drop _: rc;
    end;
run;


和谐拯救危机

7
老师她摸我 发表于 2012-12-11 10:49:42
  1. data test;
  2.         input type $ year sale;
  3.         cards;
  4. A 1 5
  5. B 2 6
  6. A 2 7
  7. B 1 3
  8. ;
  9. run;
  10. proc sort data=test out=test1;
  11.         by type year;
  12. run;
  13. data test2;
  14.         set test1;
  15.         by type year;
  16.         if first.type then cum_sale=sale;
  17.                 else cum_sale+sale;
  18. run;
复制代码

已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
a6566792 + 1 + 1 + 1 热心帮助其他会员
Imasasor + 12 + 24 + 1 + 1 + 1 观点有启发

总评分: 经验 + 12  论坛币 + 24  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

欢迎加入SAS群:144839730-蜗牛

8
さsけ 发表于 2012-12-11 13:31:54
proc sql;                                                                                                                              
        select id,type,year,sale,(select sum(sale) from test where id>=c.id and type=c.type and year=c.year ) as cum_val                                
        from test c;                                                                                                                    
quit;  
增加一列ID,相当于观测值。。

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-5 05:15