全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
4290 7
2012-12-11
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
大大们,请问这代码该如何写呢
二维码

扫码加我 拉你入群

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

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

全部回复
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)
二维码

扫码加我 拉你入群

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

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

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.
二维码

扫码加我 拉你入群

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

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

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)
二维码

扫码加我 拉你入群

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

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

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。而且我的表是很大的,没可能一个个写吧
二维码

扫码加我 拉你入群

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

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

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;


二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群