楼主: claudiehuang
1548 13

[问答] 求助,如何分组给每组里面的数据编号 [推广有奖]

  • 0关注
  • 0粉丝

硕士生

47%

还不是VIP/贵宾

-

威望
0
论坛币
5 个
通用积分
0
学术水平
0 点
热心指数
9 点
信用等级
0 点
经验
1612 点
帖子
71
精华
0
在线时间
157 小时
注册时间
2017-10-25
最后登录
2021-4-17

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
求助我的数据有人和月份,如果我想给每一个人(personid),然后他们月份相差不超过3个月就assign一个sequence number, 如果超过3个月就sequence number +1, 这样的loop怎么写啊。
比如我的数据是这样
1.jpg
我希望它变成这样
2.jpg
谢谢各位大神!
二维码

扫码加我 拉你入群

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

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

关键词:sequence Number Person assign sign SAS 求助! sas 求助 SAS编程技术教程 SAS应用 SAS数据分析方法

沙发
Lee_iris 在职认证  学生认证  发表于 2021-2-13 17:22:07 来自手机 |只看作者 |坛友微信交流群
claudiehuang 发表于 2021-2-13 13:53
求助我的数据有人和月份,如果我想给每一个人(personid),然后他们月份相差不超过3个月就assign一个seque ...
提供一个可能的思路,
如果表示年月的变量是文本格式的,可以截取后2位,然后分企业将月份值迭代相减,
再设置,如果这个差值等于多少,数值为几

使用道具

藤椅
superguy333 发表于 2021-2-13 19:46:56 |只看作者 |坛友微信交流群
  1. data x1;
  2. input personid 3.0  year_month $10.;
  3. cards;
  4. 1  199001
  5. 1  199002
  6. 1  199005
  7. 1  199007
  8. 1  199008
  9. 1  199210
  10. 1  199212
  11. 1  199301
  12. 1  199302
  13. 2  199003
  14. 2  199004
  15. 2  199006
  16. 2  199202
  17. 2  199204
  18. ;
  19. run;

  20. proc sort data =x1;
  21. by personid  year_month;
  22. run;
  23. data x2;
  24. set x1;
  25. year_month1=lag(year_month);
  26. month_diff=(substr(year_month,1,4)-substr(year_month1,1,4))*12+substr(year_month,5,2)-substr(year_month1,5,2);  
  27. run;

  28. data x3;
  29. set x2;
  30. retain seq ;
  31. by personid;
  32. if first.personid then seq=1;
  33. if not first.personid and month_diff>=3 then seq=seq+1;
  34. DROP year_month1 month_diff;
  35. run;
复制代码
已有 1 人评分经验 收起 理由
eijuhz + 20 精彩帖子

总评分: 经验 + 20   查看全部评分

使用道具

板凳
claudiehuang 发表于 2021-2-14 09:04:50 |只看作者 |坛友微信交流群
大神们拜托看一看~

使用道具

报纸
乐天天12300 发表于 2021-2-14 12:17:30 |只看作者 |坛友微信交流群
  1. data test;
  2.   input personid x;
  3. cards;
  4. 1 199001
  5. 1 199002
  6. 1 199006
  7. 1 199008
  8. 2 199001
  9. 2 199002
  10. 2 199006
  11. 2 199008
  12. ;
  13. run;

  14. *假设你的数据已经按照id和x排序;
  15. data test1;
  16.    set test;
  17.    by personid;
  18.    if first.personid then seq=1;
  19.    if abs(x - lag(x))>=3 then
  20.       do;
  21.          if first.personid then seq=1;
  22.          else  
  23.              do;
  24.                seq +1;
  25.                      end;
  26.           end;
  27. run;
复制代码
已有 1 人评分经验 收起 理由
eijuhz + 20 精彩帖子

总评分: 经验 + 20   查看全部评分

使用道具

地板
Lee_iris 在职认证  学生认证  发表于 2021-2-15 11:35:36 |只看作者 |坛友微信交流群
先按企业和时间排序,然后分企业把时间相减,得到相差的月份,然后按你的要求新生成变量,不用写循环代码吧,时间怎么相减可以再百度下

使用道具

7
superguy333 发表于 2021-2-15 11:51:27 |只看作者 |坛友微信交流群
  1. data x1;
  2. input personid 3.0  year_month $10.;
  3. cards;
  4. 1  199001
  5. 1  199002
  6. 1  199005
  7. 1  199007
  8. 1  199008
  9. 1  199210
  10. 1  199212
  11. 1  199301
  12. 1  199302
  13. 2  199003
  14. 2  199004
  15. 2  199006
  16. 2  199202
  17. 2  199204
  18. ;
  19. run;

  20. proc sort data =x1;
  21. by personid  year_month;
  22. run;
  23. data x2;
  24. set x1;
  25. year_month1=lag(year_month);
  26. month_diff=(substr(year_month,1,4)-substr(year_month1,1,4))*12+substr(year_month,5,2)-substr(year_month1,5,2);  
  27. run;

  28. data x3;
  29. set x2;
  30. retain seq ;
  31. by personid;
  32. if first.personid then seq=1;
  33. if not first.personid and month_diff>=3 then seq=seq+1;
  34. DROP year_month1 month_diff;
  35. run;
复制代码
已有 1 人评分经验 收起 理由
eijuhz + 20 精彩帖子

总评分: 经验 + 20   查看全部评分

使用道具

8
superguy333 发表于 2021-2-15 13:52:00 |只看作者 |坛友微信交流群
  1. data x1;
  2. input personid 3.0  year_month $10.;
  3. cards;
  4. 1  199001
  5. 1  199002
  6. 1  199005
  7. 1  199007
  8. 1  199008
  9. 1  199210
  10. 1  199212
  11. 1  199301
  12. 1  199302
  13. 2  199003
  14. 2  199004
  15. 2  199006
  16. 2  199202
  17. 2  199204
  18. ;
  19. run;

  20. proc sort data =x1;
  21. by personid  year_month;
  22. run;
  23. data x2;
  24. set x1;
  25. year_month1=lag(year_month);
  26. month_diff=(substr(year_month,1,4)-substr(year_month1,1,4))*12+substr(year_month,5,2)-substr(year_month1,5,2);  
  27. run;

  28. data x3;
  29. set x2;
  30. retain seq ;
  31. by personid;
  32. if first.personid then seq=1;
  33. if not first.personid and month_diff>=3 then seq=seq+1;
  34. DROP year_month1 month_diff;
  35. run;
复制代码
已有 1 人评分经验 收起 理由
eijuhz + 20 精彩帖子

总评分: 经验 + 20   查看全部评分

使用道具

9
superguy333 发表于 2021-2-15 19:17:10 |只看作者 |坛友微信交流群
  1. /*录入数据*/
  2. data x1;
  3. input personid 3.0  year_month $10.;
  4. cards;
  5. 1  199001
  6. 1  199002
  7. 1  199005
  8. 1  199007
  9. 1  199008
  10. 1  199210
  11. 1  199212
  12. 1  199301
  13. 1  199302
  14. 2  199003
  15. 2  199004
  16. 2  199006
  17. 2  199202
  18. 2  199204
  19. ;
  20. run;

  21. /*排序*/
  22. proc sort data =x1;
  23. by personid  year_month;
  24. run;

  25. /*计算月差month_diff*/
  26. data x2;
  27. set x1;
  28. year_month1=lag(year_month);
  29. month_diff=(substr(year_month,1,4)-substr(year_month1,1,4))*12+substr(year_month,5,2)-substr(year_month1,5,2);  
  30. run;

  31. /*retain语句 定义变量seq,1、首次出现personid时,seq=1;
  32.                           2、非首次出现personid并且月差≥3时,seq自动增加1*/
  33. data x3;
  34. set x2;
  35. retain seq ;
  36. by personid;
  37. if first.personid then seq=1;
  38. if not first.personid and month_diff>=3 then seq=seq+1;
  39. DROP year_month1 month_diff;
  40. run;
复制代码
已有 1 人评分经验 收起 理由
eijuhz + 20 精彩帖子

总评分: 经验 + 20   查看全部评分

使用道具

10
superguy333 发表于 2021-2-15 19:17:42 |只看作者 |坛友微信交流群
claudiehuang 发表于 2021-2-14 09:04
大神们拜托看一看~
  1. /*录入数据*/
  2. data x1;
  3. input personid 3.0  year_month $10.;
  4. cards;
  5. 1  199001
  6. 1  199002
  7. 1  199005
  8. 1  199007
  9. 1  199008
  10. 1  199210
  11. 1  199212
  12. 1  199301
  13. 1  199302
  14. 2  199003
  15. 2  199004
  16. 2  199006
  17. 2  199202
  18. 2  199204
  19. ;
  20. run;

  21. /*排序*/
  22. proc sort data =x1;
  23. by personid  year_month;
  24. run;

  25. /*计算月差month_diff*/
  26. data x2;
  27. set x1;
  28. year_month1=lag(year_month);
  29. month_diff=(substr(year_month,1,4)-substr(year_month1,1,4))*12+substr(year_month,5,2)-substr(year_month1,5,2);  
  30. run;

  31. /*retain语句 定义变量seq,1、首次出现personid时,seq=1;
  32.                           2、非首次出现personid并且月差≥3时,seq自动增加1*/
  33. data x3;
  34. set x2;
  35. retain seq ;
  36. by personid;
  37. if first.personid then seq=1;
  38. if not first.personid and month_diff>=3 then seq=seq+1;
  39. DROP year_month1 month_diff;
  40. run;
复制代码

使用道具

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

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

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

GMT+8, 2024-4-25 13:21