请选择 进入手机版 | 继续访问电脑版
楼主: Rock2000
1054 3

[问答] 求助:变量纵向累加错误 [推广有奖]

  • 1关注
  • 24粉丝

学术权威

22%

还不是VIP/贵宾

-

威望
1
论坛币
104811 个
通用积分
25.5412
学术水平
55 点
热心指数
74 点
信用等级
48 点
经验
23619 点
帖子
547
精华
0
在线时间
10767 小时
注册时间
2004-5-27
最后登录
2024-3-28

Rock2000 发表于 2017-7-19 09:58:33 |显示全部楼层 |坛友微信交流群
相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

以SASHELP.CLASS数据为例,以sex变量分组对变量weight进行分别累加,程序如下,但是没有达到要求,错在哪里?
  1. data class; set sashelp.class; run;

  2. proc sort data=class; by sex; run;

  3. data test;
  4. set class;
  5. by sex;
  6. if first.sex then do;
  7. cum_weight=weight;
  8. cut_weight=1;
  9. avg_weight=.;
  10. end;
  11. cum_weight=cum_weight+weight;
  12. cut_weight=cut_weight+1;
  13. avg_weight=cum_weight/cut_weight;
  14. run;
  15. proc print data=test; run;
复制代码


结果


二维码

扫码加我 拉你入群

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

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

关键词:SASHELP Weight Eight weigh print

1.png
Rock2000 发表于 2017-7-19 10:17:12 |显示全部楼层 |坛友微信交流群
自己重新修改了程序,搞定了。
  1. data class; set sashelp.class; run;
  2. proc sort data=class; by sex; run;

  3. data test;
  4. set class;
  5. by sex;
  6. if first.sex then cum_weight=.;
  7. if first.sex then cnt_weight=0;
  8. if first.sex then avg_weight=weight;
  9. cum_weight+weight;
  10. cnt_weight+1;
  11. avg_weight=cum_weight/cnt_weight;
  12. run;
  13. proc print data=test; run;
复制代码


使用道具

  1.     data test;
  2.     set class;
  3.     by sex;

  4.     if first.sex then do;
  5.     cum_weight=0;
  6.     cut_weight=0;
  7.     avg_weight=0;
  8.     end;

  9.     cum_weight+weight;
  10.     cut_weight+1;
  11.     avg_weight=cum_weight/cut_weight;

  12.         if last.sex then;
  13.     run;
复制代码
已有 1 人评分论坛币 学术水平 热心指数 收起 理由
Rock2000 + 5 + 1 + 5 精彩帖子

总评分: 论坛币 + 5  学术水平 + 1  热心指数 + 5   查看全部评分

使用道具

Rock2000 发表于 2017-7-19 19:19:51 |显示全部楼层 |坛友微信交流群
大片阳光因你 发表于 2017-7-19 10:21
谢谢,请看信息提示,谢谢。

使用道具

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

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

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

GMT+8, 2024-3-29 15:45