楼主: binbin0570
2692 5

sas中分类剔除某类变量中某时间点之前的记录 [推广有奖]

  • 2关注
  • 0粉丝

硕士生

48%

还不是VIP/贵宾

-

威望
0
论坛币
142 个
通用积分
1.7355
学术水平
3 点
热心指数
7 点
信用等级
0 点
经验
2013 点
帖子
53
精华
0
在线时间
257 小时
注册时间
2014-3-13
最后登录
2023-12-29

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
在sas中如何实现,剔除某个id中某一时间点之前的记录,所有id中都要进行剔除。比如id    time               mark
a    2011-1-1        .
a    2011-2-1        .
a    2011-3-1        1
a    2011-4-1        .
a    2011-5-1        1
b    2011-2-1        .
b    2011-3-1        1
b    2011-4-1        .
b    2011-5-1        1


想要得到以下数据,即Mark中第一次有1出现的每个id之后的记录
id    time               mark
a    2011-3-1        1
a    2011-4-1        .
a    2011-5-1        1

b    2011-3-1        1
b    2011-4-1        .
b    2011-5-1        1

二维码

扫码加我 拉你入群

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

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

关键词:Mark time 如何实现 Mar Tim 记录

沙发
lovexialulu 发表于 2017-3-16 15:42:51 |只看作者 |坛友微信交流群
  1. data a;
  2. input id $1. time $10. mark 8.  @;
  3. cards;
  4. a 2011-1-1 .
  5. a 2011-2-1 .
  6. a 2011-3-1 1
  7. a 2011-4-1 .
  8. a 2011-5-1 1
  9. b 2011-2-1 .
  10. b 2011-3-1 1
  11. b 2011-4-1 .
  12. b 2011-5-1 1
  13. c 2012-3-1 .
  14. c 2013-12-1 1
  15. c 2012-5-1 .
  16. ;
  17. run;

  18. data b;
  19. set a;
  20. tim=input(time,anydtdte.);
  21. format tim date9.;
  22. proc sort;by id time;
  23. run;
  24. data c;
  25. set b;
  26. by id tim;
  27. retain mm;
  28. if mark=1 then mm=1;
  29. if first.id and mark=. then mm=0;
  30. if mm ne 0;
  31. run;
复制代码

time.PNG (14.21 KB)

time.PNG

使用道具

藤椅
zwnSAS121 发表于 2017-3-17 11:23:58 |只看作者 |坛友微信交流群
  1. data aa;
  2. informat time yymmdd10.;
  3. input id$ time mark;
  4. format time yymmdd10.;
  5. cards;
  6. a    2011-01-01        .
  7. a    2011-02-01        .
  8. a    2011-03-01        1
  9. a    2011-04-01        .
  10. a    2011-05-01        1
  11. b    2011-02-01        .
  12. b    2011-03-01        1
  13. b    2011-04-01        .
  14. b    2011-05-01        1
  15. c    2011-06-02        .
  16. c    2011-06-03        .
  17. c    2011-07-01        1
  18. c    2011-08-01        .
  19. c    2011-09-01        1
  20. ;

  21. proc sql noprint;
  22. select count(distinct id)
  23.       Into :N
  24.       from aa;
  25. select distinct id
  26.       into :type1-:type%left(&N)  
  27.       from aa;
  28. quit;

  29. %macro split;

  30. %do i=1 %to &N;

  31. /*安照条件分割输出数据集*/
  32. data id_&&type&i;
  33. set aa;
  34. if id="&&type&i" then output id_&&type&i;
  35. run;

  36. /*对每一个数据集进行retain函数复制值的应用*/
  37. data id_&&type&i;
  38. retain mark1;
  39. set id_&&type&i;
  40. if mark ^=. then mark1=mark;/*forward copy*/
  41. if mark1=1;
  42. run;

  43. /*合并数据集*/
  44. data aaa;
复制代码

使用道具

板凳
zwnSAS121 发表于 2017-3-17 13:26:12 |只看作者 |坛友微信交流群
  1. data aa;
  2. informat time yymmdd10.;
  3. input id$ time mark;
  4. format time yymmdd10.;
  5. cards;
  6. a    2011-01-01        .
  7. a    2011-02-01        .
  8. a    2011-03-01        1
  9. a    2011-04-01        .
  10. a    2011-05-01        1
  11. b    2011-02-01        .
  12. b    2011-03-01        1
  13. b    2011-04-01        .
  14. b    2011-05-01        1
  15. c    2011-06-02        .
  16. c    2011-06-03        .
  17. c    2011-07-01        1
  18. c    2011-08-01        .
  19. c    2011-09-01        1
  20. ;

  21. proc sql noprint;
  22. select count(distinct id)
  23.       Into :N
  24.       from aa;
  25. select distinct id
  26.       into :type1-:type%left(&N)  
  27.       from aa;
  28. quit;

  29. %macro split;

  30. %do i=1 %to &N;

  31. /*安照条件分割输出数据集*/
  32. data id_&&type&i;
  33. set aa;
  34. if id="&&type&i" then output id_&&type&i;
  35. run;

  36. /*对每一个数据集进行retain函数复制值的应用*/
  37. data id_&&type&i;
  38. retain mark1;
  39. set id_&&type&i;
  40. if mark ^=. then mark1=mark;/*forward copy*/
  41. if mark1=1;
  42. run;

  43. %end;
复制代码

使用道具

报纸
binbin0570 发表于 2017-3-20 15:14:51 |只看作者 |坛友微信交流群
之前已经解决,不过还是非常感谢您的帮助

使用道具

地板
touey 发表于 2017-5-15 20:34:27 来自手机 |只看作者 |坛友微信交流群
请教是如何实现呢?

使用道具

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

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

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

GMT+8, 2024-4-20 23:16