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

I have dataset with same account number assigned to different family members.  There can be one row or many rows per account number.    I need to keep up to the first 3 row by account number.   If there's only 1 row by account number, I need to keep it.    If there 7 rows by account number, then I need to keep only first 3 rows.    Here is example of the data and example of what I need.  Thank you in advance for the help.

 

 ACCOUNT_NUMBERFIRST_NMLAST_NM
 1234JOHNJONES
 1234SUEJONES
 1234MIKEJONES
 1234DAVIDJONES
DATA4321JAMESRICE
 4321MARYRICE
 5261BARBARAFRY
 5261RUSSFRY
 5261GEOFFREYFRY
 5261BRYONFRY
 5261DONNAFRY
 5261SUSANNEFRY
 5261PETERFRY
 7412TODDBLUE

 

 

 ACCOUNT_NUMBERFIRST_NMLAST_NM
 1234JOHNJONES
 1234SUEJONES
 1234MIKEJONES
 4321JAMESRICE
NEED4321MARYRICE
 5261BARBARAFRY
 5261RUSSFRY
 5261GEOFFREYFRY
 7412TODDBLUE
    
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input ACCOUNT_NUMBER FIRST_NM $ LAST_NM $;
datalines;
1234 JOHN JONES
1234 SUE JONES
1234 MIKE JONES
1234 DAVID JONES
4321 JAMES RICE
4321 MARY RICE
5261 BARBARA FRY
5261 RUSS FRY
5261 GEOFFREY FRY
5261 BRYON FRY
5261 DONNA FRY
5261 SUSANNE FRY
5261 PETER FRY
7412 TODD BLUE
;

data want(drop=c);
    set have;
    by ACCOUNT_NUMBER;
    if first.ACCOUNT_NUMBER then c=0;
    c+1;
    if c <= 3;
run;

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input ACCOUNT_NUMBER FIRST_NM $ LAST_NM $;
datalines;
1234 JOHN JONES
1234 SUE JONES
1234 MIKE JONES
1234 DAVID JONES
4321 JAMES RICE
4321 MARY RICE
5261 BARBARA FRY
5261 RUSS FRY
5261 GEOFFREY FRY
5261 BRYON FRY
5261 DONNA FRY
5261 SUSANNE FRY
5261 PETER FRY
7412 TODD BLUE
;

data want(drop=c);
    set have;
    by ACCOUNT_NUMBER;
    if first.ACCOUNT_NUMBER then c=0;
    c+1;
    if c <= 3;
run;
marcus7w
Obsidian | Level 7

Almost,  the results only kept the first 3 rows of entire dataset.    I need to keep up to 3 rows for every same account_number.  

 

cACCOUNT_NUMBERFIRST_NMLAST_NM
11234JOHNJONES
21234SUEJONES
31234MIKEJONES
marcus7w
Obsidian | Level 7

Almost,  the results only kept the first 3 rows of entire dataset.    I need to keep up to 3 rows for every same account_number.  

 

RESULT:

cACCOUNT_NUMBERFIRST_NMLAST_NM 
11234JOHNJONES 
21234SUEJONES 
31234MIKEJONES 

 

NEED:

COUNTACCOUNT_NUMBERFIRST_NMLAST_NM
11234JOHNJONES
21234SUEJONES
31234MIKEJONES
14321JAMESRICE
24321MARYRICE
15261BARBARAFRY
25261RUSSFRY
35261GEOFFREYFRY
17412TODDBLUE
marcus7w
Obsidian | Level 7

You're correct,  I had typo. It works.  Thank you for your help,  much appreciated.  

Kurt_Bremser
Super User

@marcus7w wrote:

Almost,  the results only kept the first 3 rows of entire dataset.    I need to keep up to 3 rows for every same account_number.  

 

c ACCOUNT_NUMBER FIRST_NM LAST_NM
1 1234 JOHN JONES
2 1234 SUE JONES
3 1234 MIKE JONES

You obviously have not run @PeterClemmensen's code; run it, and then come back.

marcus7w
Obsidian | Level 7

My bad, first attempt, i had a typo.  @PeterClemmensen code work awesome.   Thank you for the quick reply,  much appreciated. 

s_lassen
Meteorite | Level 14

Here's one more solution, slightly simpler:

data want;
  do _N_=1 by 1 until(last.account_number);
    set have;
    by account_number;
    if _N_<=3 then output;
    end;
run;
marcus7w
Obsidian | Level 7

I tried this simpler solution, it works too.  Thank you very much for quick response!!!!

Kurt_Bremser
Super User

Since your dataset is already sorted by account_number, this is easily achieved in a data step:

data want;
set have;
by account_number;
if first.account_number
then count = 1;
else count + 1;
if count le 3;
drop count;
run;

The increment statement

count + 1;

causes count to automatically be retained.

marcus7w
Obsidian | Level 7

Thank you for your help and quick reply.  This works and very helpful!!! 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 11 replies
  • 572 views
  • 7 likes
  • 4 in conversation