BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
agosawi
Calcite | Level 5

Dear SAS Users,

I have this below table.

 

Old Account NumberNew Account NumberDate
1 20180630
1220180731
2320180731
3420180731
4520180731

 

Table contains both old and new account numbers.

New account numbers gets assigned whenever old gets transferred.

I want to select all those records where particular account transferred atleast 4 times in given month.
In above example I want to select all the rows from 2 for 20180731.

I tried using self joins but unfortunately it didn't work.

I found most difficult part is extracting only count of transfer ge 4.

 

I will really appreciate your help.

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Not very elegant, but easy to understand:

data HAVE;
 input OLD NEW ;
 cards;
1 .
1 2
2 3
3 4
4 5
5 6
7 8
9 10
11 12
run;
proc sql; 
 create table FOUR_PLUS_TRANSFER_HISTORY as
 select h1.OLD as OLD1
      , h2.OLD as OLD2
      , h3.OLD as OLD3
      , h4.OLD as OLD4
      , h4.NEW AS NEW5
 from HAVE h1
        inner join
      HAVE h2
        on h1.NEW=h2.OLD
        inner join
      HAVE h3
        on h2.NEW=h3.OLD
        inner join
      HAVE h4
        on h3.NEW=h4.OLD
 where h4.NEW ne .;

 create table WANT as
 select unique HAVE.* 
 from HAVE 
     ,FOUR_PLUS_TRANSFER_HISTORY
 where OLD = OLD1 
    or OLD = OLD2 
    or OLD = OLD3 
    or OLD = OLD4 ;

quit;

 

OLD NEW
1 .
1 2
2 3
3 4
4 5
5 6

View solution in original post

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

Not very elegant, but easy to understand:

data HAVE;
 input OLD NEW ;
 cards;
1 .
1 2
2 3
3 4
4 5
5 6
7 8
9 10
11 12
run;
proc sql; 
 create table FOUR_PLUS_TRANSFER_HISTORY as
 select h1.OLD as OLD1
      , h2.OLD as OLD2
      , h3.OLD as OLD3
      , h4.OLD as OLD4
      , h4.NEW AS NEW5
 from HAVE h1
        inner join
      HAVE h2
        on h1.NEW=h2.OLD
        inner join
      HAVE h3
        on h2.NEW=h3.OLD
        inner join
      HAVE h4
        on h3.NEW=h4.OLD
 where h4.NEW ne .;

 create table WANT as
 select unique HAVE.* 
 from HAVE 
     ,FOUR_PLUS_TRANSFER_HISTORY
 where OLD = OLD1 
    or OLD = OLD2 
    or OLD = OLD3 
    or OLD = OLD4 ;

quit;

 

OLD NEW
1 .
1 2
2 3
3 4
4 5
5 6
agosawi
Calcite | Level 5

Thank you so much!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 808 views
  • 0 likes
  • 2 in conversation