楼主: hwb5258289
787 3

[有偿编程] SAS求重复的最大数和ID唯一号 [推广有奖]

  • 3关注
  • 0粉丝

已卖:15份资源

硕士生

70%

还不是VIP/贵宾

-

威望
0
论坛币
766 个
通用积分
5.2403
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
10427 点
帖子
120
精华
0
在线时间
242 小时
注册时间
2010-5-9
最后登录
2025-11-9

楼主
hwb5258289 发表于 2025-6-14 12:05:42 |AI写论文
5论坛币
依据id和fuwu两个变量,求重复的最大数和ID_only,其中max和 id_only是想要计算的。
  1. data want;
  2. input id fuwu  freq max id_only;
  3. FORMAT fuwu yymmdd10.;
  4. cards;
  5. 1        2025/1/1        1        5        1
  6. 1        2025/3/28        2        5        1
  7. 1        2025/4/23        3        5        1
  8. 1        2025/5/19        4        5        1
  9. 1        2025/6/14        5        5        1
  10. 6        2025/1/5        1        5        2
  11. 6        2025/3/28        2        5        2
  12. 6        2025/4/23        3        5        2
  13. 6        2025/5/19        4        5        2
  14. 6        2025/6/14        5        5        2
  15. 11        2025/1/6        1        2        3
  16. 11        2025/6/14        2        2        3
  17. 13        2025/3/6        1        1        4
  18. 14        2025/2/2        1        3        5
  19. 14        2025/3/2        2        3        5
  20. 14        2025/4/2        3        3        5
  21. 17        2025/3/2        1        7        6
  22. 17        2025/3/28        2        7        6
  23. 17        2025/4/23        3        7        6
  24. 17        2025/5/19        4        7        6
  25. 17        2025/6/14        5        7        6
  26. 17        2025/7/10        6        7        6
  27. 17        2025/8/5        7        7        6

  28. ;
  29. run;
复制代码


关键词:format Input cards FORMA ONLY

沙发
如风飘扬 发表于 2025-6-17 11:40:32
试试

  1. data test;
  2.         input id fuwu freq;
  3.         informat fuwu yymmdd10.;
  4.         format fuwu yymmdd10.;
  5.         cards;
  6.                 1        2025/1/1        1
  7.                 1        2025/3/28        2
  8.                 1        2025/4/23        3
  9.                 1        2025/5/19        4
  10.                 1        2025/6/14        5
  11.                 6        2025/1/5        1
  12.                 6        2025/3/28        2
  13.                 6        2025/4/23        3
  14.                 6        2025/5/19        4
  15.                 6        2025/6/14        5
  16.                 11        2025/1/6        1
  17.                 11        2025/6/14        2
  18.                 13        2025/3/6        1
  19.                 14        2025/2/2        1
  20.                 14        2025/3/2        2
  21.                 14        2025/4/2        3
  22.                 17        2025/3/2        1
  23.                 17        2025/3/28        2
  24.                 17        2025/4/23        3
  25.                 17        2025/5/19        4
  26.                 17        2025/6/14        5
  27.                 17        2025/7/10        6
  28.                 17        2025/8/5        7
  29.         ;
  30. run;

  31. /* 计算最大重复数 */
  32. proc sql noprint;
  33.         create table want1 as
  34.         select id, fuwu, freq, max(freq) as max
  35.         from test
  36.         group by id
  37.         order by id, fuwu
  38.         ;
  39. quit;

  40. /* ID唯一号 */
  41. proc sort data=want1;
  42.         by id;
  43. run;

  44. data want(drop=i);
  45.         set want1;
  46.         by id;
  47.         i=1;
  48.         if first.id then id_only+i;
  49. run;
复制代码
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
hwb5258289 + 5 + 1 + 1 + 1 精彩帖子

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

藤椅
whymath 发表于 2025-6-25 10:58:15
衍生变量 MAX 是一个简单的自表查询问题,可以使用的方法很多。下面给出用 DOW-LOOP 的做法:
  1. data test;
  2.   input id fuwu freq;
  3.   informat fuwu yymmdd10.;
  4.   format fuwu yymmdd10.;
  5.   cards;
  6.   1        2025/1/1        1
  7.   1        2025/3/28       2
  8.   1        2025/4/23       3
  9.   1        2025/5/19       4
  10.   1        2025/6/14       5
  11.   6        2025/1/5        1
  12.   6        2025/3/28       2
  13.   6        2025/4/23       3
  14.   6        2025/5/19       4
  15.   6        2025/6/14       5
  16.   11        2025/1/6       1
  17.   11        2025/6/14      2
  18.   13        2025/3/6       1
  19.   14        2025/2/2       1
  20.   14        2025/3/2       2
  21.   14        2025/4/2       3
  22.   17        2025/3/2       1
  23.   17        2025/3/28      2
  24.   17        2025/4/23      3
  25.   17        2025/5/19      4
  26.   17        2025/6/14      5
  27.   17        2025/7/10      6
  28.   17        2025/8/5       7
  29.   ;
  30. run;

  31. data want;
  32.   do until(last.id);
  33.     set test;
  34.     by id;
  35.     max=sum(max,1);
  36.     id_only+last.id;
  37.   end;

  38.   do until(last.id);
  39.     set test;
  40.     by id;
  41.     output;
  42.   end;
  43. run;
复制代码

变量 ID_ONLY 看起来是对 ID 变量进行顺序编码,起始值是1,可以借助 BY 语句 + FIRST/LAST.VAR 语句实现。

板凳
whymath 发表于 2025-6-25 11:02:16
whymath 发表于 2025-6-25 10:58
衍生变量 MAX 是一个简单的自表查询问题,可以使用的方法很多。下面给出用 DOW-LOOP 的做法:

变量 ID_ ...
此外,我也在此前尝试过在单个数据步中衍生类似 MAX 的变量的多种方式,供你参考:https://bbs.pinggu.org/thread-6586478-2-1.html

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 05:16