在SAS Enterprise Studio中基于条件如何添加行号?

huangapple go评论105阅读模式
英文:

How to add a row number based on criteria in SAS Enterprise Studio?

问题

这是我尝试的代码:

  1. data add_row_count;
  2. set LIB.Table1;
  3. count + 1;
  4. by ID OTHER_DATE;
  5. if first.OTHER_DATE then count = 1;
  6. run;

这将根据每个唯一的 "ID" 和 "OTHER_DATE" 列的第一个出现的行设置计数器,并在每个新的 "OTHER_DATE" 值开始时将计数器重置为1。希望这可以帮助您得到期望的结果。

英文:

I have a table like this:

  1. ID OLD_ACCOUNT NEW_ACCOUNT DATE OTHER_DATE
  2. 20 001 002 1/1/2023 1/1/2023
  3. 20 002 003 2/1/2023 2/1/2023
  4. 20 002 004 4/1/2023 2/1/2023
  5. 20 003 004 4/1/2023 4/1/2023
  6. 30 101 102 1/2/2023 1/2/2023
  7. 30 102 103 2/2/2023 2/2/2023

I am trying to add a row number that starts at 1 at the first iteration of a new "OTHER_DATE" for each unique "ID". So the new column would look like this:

  1. ID OLD_ACCOUNT NEW_ACCOUNT DATE OTHER_DATE COUNT
  2. 20 001 002 1/1/2023 1/1/2023 1
  3. 20 002 003 2/1/2023 2/1/2023 1
  4. 20 002 004 4/1/2023 2/1/2023 2
  5. 20 003 004 4/1/2023 4/1/2023 1
  6. 30 101 102 1/2/2023 1/2/2023 1
  7. 30 102 103 2/2/2023 2/2/2023 1

This is the code I tried:

  1. data add_row_count ;
  2. set LIB.Table1;
  3. count + 1;
  4. by ID DATE OTHER_DATE;
  5. if first.OTHER_DATE then count = 1;
  6. run;

This is returning a 1 for every row which is not what I need. Thank you in advance

答案1

得分: 1

从BY列表中移除DATE。

  1. data have;
  2. input ID $ OLD_ACCOUNT $ NEW_ACCOUNT $ DATE OTHER_DATE;
  3. informat date other_date ddmmyy.;
  4. format date other_date yymmdd10.;
  5. cards;
  6. 20 001 002 1/1/2023 1/1/2023
  7. 20 002 003 2/1/2023 2/1/2023
  8. 20 002 004 4/1/2023 2/1/2023
  9. 20 003 004 4/1/2023 4/1/2023
  10. 30 101 102 1/2/2023 1/2/2023
  11. 30 102 103 2/2/2023 2/2/2023
  12. ;
  13. data want;
  14. set have;
  15. by id other_date;
  16. count+1;
  17. if first.other_date then count=1;
  18. run;

结果

  1. OLD_ NEW_
  2. Obs ID ACCOUNT ACCOUNT DATE OTHER_DATE count
  3. 1 20 001 002 2023-01-01 2023-01-01 1
  4. 2 20 002 003 2023-01-02 2023-01-02 1
  5. 3 20 002 004 2023-01-04 2023-01-02 2
  6. 4 20 003 004 2023-01-04 2023-01-04 1
  7. 5 30 101 102 2023-02-01 2023-02-01 1
  8. 6 30 102 103 2023-02-02 2023-02-02 1
英文:

Remove DATE from the BY list.

  1. data have ;
  2. input ID $ OLD_ACCOUNT $ NEW_ACCOUNT $ DATE OTHER_DATE;
  3. informat date other_date ddmmyy.;
  4. format date other_date yymmdd10.;
  5. cards;
  6. 20 001 002 1/1/2023 1/1/2023
  7. 20 002 003 2/1/2023 2/1/2023
  8. 20 002 004 4/1/2023 2/1/2023
  9. 20 003 004 4/1/2023 4/1/2023
  10. 30 101 102 1/2/2023 1/2/2023
  11. 30 102 103 2/2/2023 2/2/2023
  12. ;
  13. data want;
  14. set have;
  15. by id other_date;
  16. count+1;
  17. if first.other_date then count=1;
  18. run;

Result

  1. OLD_ NEW_
  2. Obs ID ACCOUNT ACCOUNT DATE OTHER_DATE count
  3. 1 20 001 002 2023-01-01 2023-01-01 1
  4. 2 20 002 003 2023-01-02 2023-01-02 1
  5. 3 20 002 004 2023-01-04 2023-01-02 2
  6. 4 20 003 004 2023-01-04 2023-01-04 1
  7. 5 30 101 102 2023-02-01 2023-02-01 1
  8. 6 30 102 103 2023-02-02 2023-02-02 1

答案2

得分: 0

这段代码是关于操作顺序的可能性。这对你有什么帮助吗?

  1. data add_row_count ;
  2. set LIB.Table1;
  3. by ID DATE OTHER_DATE;
  4. if first.OTHER_DATE then count = 1;
  5. else count+1;
  6. run;
英文:

Order of operations possibly. How does this work for you?

  1. data add_row_count ;
  2. set LIB.Table1;
  3. by ID DATE OTHER_DATE;
  4. if first.OTHER_DATE then count = 1;
  5. else count+1;
  6. run;

huangapple
  • 本文由 发表于 2023年6月29日 00:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定