BookmarkSubscribeRSS Feed
Timbim
Obsidian | Level 7

Hi Community,

 

Could you please help me on how to select only the 2nd occurrence from a dataset? Below is an example. Kind regards

 

AccountInterestRateInterestRate_ChangeDate
200015624.89%19-Nov-18
200015624.55%22-Nov-18
200069565.27%11-Sep-18
200069565.25%1-Nov-18
200069565.27%8-Nov-18
200098985.43%18-Sep-18
200098985.44%18-Oct-18
200098985.55%18-Nov-18
200098985.61%18-Dec-18
200098985.00%18-Jan-19
6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Do you mean simply the second observation? Or the second occurrence inside each account?

PeterClemmensen
Tourmaline | Level 20

 

 

data have;
input Account InterestRate :percent. InterestRate_ChangeDate :date11.;
format InterestRate percent8.2 InterestRate_ChangeDate date11.;
datalines;
20001562 4.89% 19-Nov-18
20001562 4.55% 22-Nov-18
20006956 5.27% 11-Sep-18
20006956 5.25% 1-Nov-18
20006956 5.27% 8-Nov-18
20009898 5.43% 18-Sep-18
20009898 5.44% 18-Oct-18
20009898 5.55% 18-Nov-18
20009898 5.61% 18-Dec-18
20009898 5.00% 18-Jan-19
;

data want;
	if _N_=1 then do;
		declare hash h();
		h.definekey('Account');
		h.definedata('c');
		h.definedone();
	end;

	set have;
	
	rc=h.check();
 
    if rc ne 0 then do;
       c=1;
       h.add();
    end;
    if rc=0 then do;
       rc=h.find();	c=c+1;
       if c=2 then output;;
       rc=h.replace();
    end;
run;

 

Timbim
Obsidian | Level 7
The 2nd occurrence for the account.
PeterClemmensen
Tourmaline | Level 20

Ok. Have you tried the code I posted above?

PeterClemmensen
Tourmaline | Level 20

Since you data is sorted by Account, you can also do this

 

data want;
	do _N_=1 by 1 until (last.Account);
		set have;
		by Account;
		if _N_=2 then output;
	end;
run;

With the hash object code though, the data does not have to be sorted by Account.

Kurt_Bremser
Super User
data want;
set have;
by account;
if first.account
then counter = 1;
else counter + 1;
if counter = 2;
drop counter;
run;

have needs to be sorted properly, of course.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 1460 views
  • 2 likes
  • 3 in conversation