请选择 进入手机版 | 继续访问电脑版
楼主: jackyloda
2420 19

[问答] 求助SAS有条件下的求和 [推广有奖]

jl60156 发表于 2014-12-23 12:23:22 |显示全部楼层 |坛友微信交流群
%macro test(n=);
data ex1;
        merge ex %do i=2 %to %eval(&n+1); ex(keep=b rename=(b=b&i) firstobs=&i) %end;;
        sumb=sum(of b:);
run;
proc sql noprint;
        create table want as
        select a,  sumb, sum(sumb) as sumall
        from ex1
        having a=1;
quit;
%mend;

%test(n=5);
已有 1 人评分论坛币 收起 理由
admin_kefu + 30 根据规定进行奖励

总评分: 论坛币 + 30   查看全部评分

使用道具

teqel 发表于 2014-12-23 12:25:41 |显示全部楼层 |坛友微信交流群
  1. data ex ex1(rename=(b=c) keep=b);
  2. do i =1 to 100;
  3. a=round(ranuni(0),1);
  4. b=ranuni(0);
  5. output;
  6. end;
  7. drop i;
  8. run;

  9. data want(drop=i c);
  10. set ex;
  11. sum=0;
  12. if a then do;
  13.         do i=_N_ to min(_N_+4, aa);
  14.                 set ex1 nobs=aa point=i;
  15.                 sum+c;
  16.         end;
  17. end;
  18. sum1+sum;
  19. run;
复制代码

使用道具

yongyitian 发表于 2014-12-23 12:48:17 |显示全部楼层 |坛友微信交流群
jackyloda 发表于 2014-12-23 11:23
这个倒不必,我主要是想知道算法的实现,像2L那样随机生成的做Sample就行。主要是觉得sas里每个变量的第几 ...
  1. /* It is not easy to understant what do you want */
  2. /* the following code produce one number of sum */

  3. data want;
  4.   array aa{200} _temporary_;
  5.   array bb{200} _temporary_;
  6.   if _n_ = 1 then do;
  7.      i = 1;
  8.    do until (last);
  9.       set ex end=last;
  10.         aa[i] = a;
  11.         bb[i] = b;
  12.      i+1;
  13.      end;
  14.      end;
  15.    sum_a=0;
  16.     do i = 1 to 200;
  17.       a = aa[i]; b=bb[i];
  18.     if a = 1 then do;
  19.         sum_6b= sum(bb[i], bb[i+1], bb[i+2], bb[i+3],bb[i+4], bb[i+5]) ;
  20.         sum_a + sum_6b;
  21.         put i= aa[i]= sum_6b=  sum_a=;
  22.     end;
  23.     end;
  24. run;
复制代码

使用道具

mingfeng07 学生认证  发表于 2014-12-23 13:32:36 |显示全部楼层 |坛友微信交流群
  1. /*举个例子*/
  2. data a;
  3. input A B @@;
  4. cards;
  5. 1 1 1 2 1 3 1 4 1 5 1 8 1 3
  6. 0 2 0 3
  7. ;
  8. run;
  9. data b;
  10. set a;
  11. id=_n_;
  12. run;
  13. proc sql;
  14. create table c as select b.id,b.A,b.B,b1.B as B1 from b,b as b1 where 0<=b1.id-b.id<=5 and b.A=b1.A and b.A=1 order by b.id;
  15. create table d as select distinct id,A,B,sum(B1) as sum_5,count(*) as count
  16.                   from c group by A,id; /*计算A=1时相应B在这个位置的值与后5个的B的和,并统计个数,注意后面有些未满6个,特用count标注*/
  17. create table e as select distinct A,sum(sum_5) as sum_all
  18.                   from d;/*最终结果,注意其中包括未满6个的B对应的和,如果需要删除不满足条件的count,可以考虑加where count<=6*/
  19. quit;
复制代码

使用道具

ziyenano 发表于 2014-12-23 13:53:42 |显示全部楼层 |坛友微信交流群
jackyloda 发表于 2014-12-23 09:42
你好,第二段的代码我还不是太理解,你是一个什么思路呢,g是一个数组吗?
g是一个矩阵,也可以当数组来理解,loc函数找出所有A=1的位置,比如有一个位置在N,求N:N+5数据的和,
当然也要考虑N+5与总长度的大小,如果N+5超过总长度会报错,所以取两个之间较小的那个

使用道具

jackyloda 发表于 2014-12-23 15:34:48 |显示全部楼层 |坛友微信交流群
李会超 发表于 2014-12-23 12:03
这个用二楼那哥们的pro iml肯定更简洁,只是很多人不太懂iml模块,我这个思路容易理解,就是代码过长,如 ...
可以简单介绍下Iml模块的思路吗,或者哪里有比较简单介绍这个的,我身边的SAS书都没讲到这个

使用道具

jackyloda 发表于 2014-12-24 10:43:17 |显示全部楼层 |坛友微信交流群
ziyenano 发表于 2014-12-22 23:50
data ex;
do i =1 to 100;
a=round(ranuni(0),1);
非常感谢你的答案,对我帮助很大!还有几个问题麻烦解答下
res=j(ncol(location),1,0);
这个j是什么意思呢,另外ncol是什么函数?

使用道具

ziyenano 发表于 2014-12-24 12:23:04 |显示全部楼层 |坛友微信交流群
jackyloda 发表于 2014-12-24 10:43
非常感谢你的答案,对我帮助很大!还有几个问题麻烦解答下
res=j(ncol(location),1,0);
这个j是什么意思 ...
j(m,n,a)生成m行 n列 元素全为a的矩阵,ncol(M)返回矩阵M的行数

使用道具

jackyloda 发表于 2014-12-24 19:01:50 |显示全部楼层 |坛友微信交流群
ziyenano 发表于 2014-12-24 12:23
j(m,n,a)生成m行 n列 元素全为a的矩阵,ncol(M)返回矩阵M的行数
了解了,多谢!以后有SAS问题再多多请教你!

使用道具

jackyloda 发表于 2015-1-7 20:25:50 |显示全部楼层 |坛友微信交流群
ziyenano 发表于 2014-12-24 12:23
j(m,n,a)生成m行 n列 元素全为a的矩阵,ncol(M)返回矩阵M的行数
你好,如果现在问题变为还有一个C列也想求和,代码能写成类似这样么,
g[location,2]+g[location:location+5,3][+]这种形式吗?

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-17 06:20