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!

AI Navigator GIF.gif


Navigate AI governance with SAS tools for compliance and ethical AI practices. Try SAS Ai Navigator for free for the first six months.

Check it out on Microsoft Marketplace →

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
  • 1990 views
  • 0 likes
  • 2 in conversation