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

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

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;

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:

确定