楼主: Rock2000
1216 3

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

  • 1关注
  • 24粉丝

已卖:6892份资源

学术权威

23%

还不是VIP/贵宾

-

威望
1
论坛币
104790 个
通用积分
51.9656
学术水平
55 点
热心指数
74 点
信用等级
48 点
经验
23510 点
帖子
520
精华
0
在线时间
10924 小时
注册时间
2004-5-27
最后登录
2025-11-12

楼主
Rock2000 发表于 2017-7-19 09:58:33 |AI写论文

+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 (126.17 KB)

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;
复制代码


藤椅
大片阳光因你 发表于 2017-7-19 10:21:45
  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
拉您进交流群
GMT+8, 2025-12-29 14:46