楼主: dxystata
1095 3

纵向数据求值 [推广有奖]

版主

大师

34%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
182304 个
通用积分
15205.6031
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
291157 点
帖子
5375
精华
1
在线时间
13478 小时
注册时间
2006-6-21
最后登录
2024-4-26

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

50论坛币

  1. data aaa;
  2. input id visit x;
  3. cards;
  4. 1 1 3
  5. 1 2 3
  6. 1 3 2
  7. 1 4 1
  8. 1 5 0
  9. 2 1 2
  10. 2 2 2
  11. 2 3 1
  12. 2 4 0
  13. 2 5 0
  14. 3 1 1
  15. 3 2 1
  16. 3 3 1
  17. 3 4 0
  18. 3 5 0
  19. ;
  20. run;
复制代码


每个个体的值y计算如下:
0.5*(visit=1的x值+visit=2的x值)+0.5*(visit=2的x值+visit=3的x值)+0.5*(visit=3的x值+visit=4的x值)+0.5*(visit=4的x值+visit=5的x值)

最终希望得到的数据
id y
1  
2  值
3  
或者
id visit x y
1 1 3  .
1 2 3 .
1 3 2 .
1 4 1 .
1 5 0  值
2 1 2 .
2 2 2 .
2 3 1 .
2 4 0 .
2 5 0
3 1 1 .
3 2 1 .
3 3 1 .
3 4 0 .
3 5 0 .

谢谢!

关键词:纵向数据 Visit Input cards visi visit
沙发
wwang111 发表于 2016-12-26 12:19:06 |只看作者 |坛友微信交流群
proc sort data=aaa out=aaa1;
by id visit;
run;

data wanted;
set aaa1;
by id visit;
lagx=lag(x);
if first.id then call missing(lagx);
if lagx ne . then mean=mean(x,lagx);
if first.id then y=0;
  y+mean;
if last.id;
keep id y;
run;

使用道具

藤椅
不是吧258123 发表于 2016-12-26 13:41:11 |只看作者 |坛友微信交流群
proc sort data=aaa;
by id;
run;
data b;
set aaa;
by id;
if first.id then temp=0.5*x;
else if last.id then temp=0.5*x;
else temp=x;
run;
proc sql;
create table total as
select id,sum(temp) as y
  from b
   group by id ;
  quit;

最后的total就是你想要的结果

使用道具

板凳
learsaas 发表于 2016-12-28 21:55:45 |只看作者 |坛友微信交流群
方法太多了

使用道具

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

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

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

GMT+8, 2024-4-26 20:54