英文:
How to add a row number based on criteria in SAS Enterprise Studio?
问题
这是我尝试的代码:
data add_row_count;
set LIB.Table1;
count + 1;
by ID OTHER_DATE;
if first.OTHER_DATE then count = 1;
run;
这将根据每个唯一的 "ID" 和 "OTHER_DATE" 列的第一个出现的行设置计数器,并在每个新的 "OTHER_DATE" 值开始时将计数器重置为1。希望这可以帮助您得到期望的结果。
英文:
I have a table like this:
ID OLD_ACCOUNT NEW_ACCOUNT DATE OTHER_DATE
20 001 002 1/1/2023 1/1/2023
20 002 003 2/1/2023 2/1/2023
20 002 004 4/1/2023 2/1/2023
20 003 004 4/1/2023 4/1/2023
30 101 102 1/2/2023 1/2/2023
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:
ID OLD_ACCOUNT NEW_ACCOUNT DATE OTHER_DATE COUNT
20 001 002 1/1/2023 1/1/2023 1
20 002 003 2/1/2023 2/1/2023 1
20 002 004 4/1/2023 2/1/2023 2
20 003 004 4/1/2023 4/1/2023 1
30 101 102 1/2/2023 1/2/2023 1
30 102 103 2/2/2023 2/2/2023 1
This is the code I tried:
data add_row_count ;
set LIB.Table1;
count + 1;
by ID DATE OTHER_DATE;
if first.OTHER_DATE then count = 1;
run;
This is returning a 1 for every row which is not what I need. Thank you in advance
答案1
得分: 1
从BY列表中移除DATE。
data have;
input ID $ OLD_ACCOUNT $ NEW_ACCOUNT $ DATE OTHER_DATE;
informat date other_date ddmmyy.;
format date other_date yymmdd10.;
cards;
20 001 002 1/1/2023 1/1/2023
20 002 003 2/1/2023 2/1/2023
20 002 004 4/1/2023 2/1/2023
20 003 004 4/1/2023 4/1/2023
30 101 102 1/2/2023 1/2/2023
30 102 103 2/2/2023 2/2/2023
;
data want;
set have;
by id other_date;
count+1;
if first.other_date then count=1;
run;
结果
OLD_ NEW_
Obs ID ACCOUNT ACCOUNT DATE OTHER_DATE count
1 20 001 002 2023-01-01 2023-01-01 1
2 20 002 003 2023-01-02 2023-01-02 1
3 20 002 004 2023-01-04 2023-01-02 2
4 20 003 004 2023-01-04 2023-01-04 1
5 30 101 102 2023-02-01 2023-02-01 1
6 30 102 103 2023-02-02 2023-02-02 1
英文:
Remove DATE from the BY list.
data have ;
input ID $ OLD_ACCOUNT $ NEW_ACCOUNT $ DATE OTHER_DATE;
informat date other_date ddmmyy.;
format date other_date yymmdd10.;
cards;
20 001 002 1/1/2023 1/1/2023
20 002 003 2/1/2023 2/1/2023
20 002 004 4/1/2023 2/1/2023
20 003 004 4/1/2023 4/1/2023
30 101 102 1/2/2023 1/2/2023
30 102 103 2/2/2023 2/2/2023
;
data want;
set have;
by id other_date;
count+1;
if first.other_date then count=1;
run;
Result
OLD_ NEW_
Obs ID ACCOUNT ACCOUNT DATE OTHER_DATE count
1 20 001 002 2023-01-01 2023-01-01 1
2 20 002 003 2023-01-02 2023-01-02 1
3 20 002 004 2023-01-04 2023-01-02 2
4 20 003 004 2023-01-04 2023-01-04 1
5 30 101 102 2023-02-01 2023-02-01 1
6 30 102 103 2023-02-02 2023-02-02 1
答案2
得分: 0
这段代码是关于操作顺序的可能性。这对你有什么帮助吗?
data add_row_count ;
set LIB.Table1;
by ID DATE OTHER_DATE;
if first.OTHER_DATE then count = 1;
else count+1;
run;
英文:
Order of operations possibly. How does this work for you?
data add_row_count ;
set LIB.Table1;
by ID DATE OTHER_DATE;
if first.OTHER_DATE then count = 1;
else count+1;
run;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论