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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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