BookmarkSubscribeRSS Feed
Sirisha1520
Calcite | Level 5
I have a requirement to get highest 3 rows for respective policy and effective date. Key is policy and effective date. am trying using group by clause having count(*) <=3, but its avoiding rows having more than 3 in input file. Please suggest how to write query
12 REPLIES 12
PaigeMiller
Diamond | Level 26

@Sirisha1520 wrote:
am trying using group by clause having count(*) <=3, but its avoiding rows having more than 3 in input file. 

Shouldn't that be count(*)>=3 ?

--
Paige Miller
ballardw
Super User

Since "highest 3", assuming you mean largest or greatest, is in effect an order statistic Proc SQL is likely not the best approach in general.

 

Proc means documentation has exactly an example of finding the three largest values by grouping variable:

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/n0afy7c5m16m9dn1s60z9krjmwxq.htm

 

The key parts are the Idgroup, max (statistic to select with) and out= (number of greatest values to select)

PaigeMiller
Diamond | Level 26

PROC RANK is probably the best way to do this, as it gives you options on how to handle things in the case where there are ties.

--
Paige Miller
Sirisha1520
Calcite | Level 5
I tried rank but that was giving 1.2, 2.5 , 4.5… so again causing duplicates as i have multple rows for policy with same effective date. Also i have sorted the file for descending order in date due to which taking count less than 3. I need first 3 records for policy with different date. Example below:

0120210101 B
0120210101 A
0120210201 C
0120210301 C

O/p expected:

0120210101 B
0120210201 C
0120210301 C
ballardw
Super User


Don't see any obvious date so "first 3" looks meaningless.

 

Example starting data helps.

Example output for that input helps more.

 

Sirisha1520
Calcite | Level 5
Policy Date Plan
01 20210101 B
01 20210101 A
01 20210201 C
01 20210301 C

O/p expected:

01 20210101 B
01 20210201 C
01 20210301 C
PaigeMiller
Diamond | Level 26

@Sirisha1520 wrote:
Policy Date Plan
01 20210101 B
01 20210101 A
01 20210201 C
01 20210301 C

O/p expected:

01 20210101 B
01 20210201 C
01 20210301 C

This does not explain the dates. Are they YYYYMMDD or YYYYDDMM?

--
Paige Miller
PaigeMiller
Diamond | Level 26

@Sirisha1520 wrote:
I tried rank but that was giving 1.2, 2.5 , 4.5… so again causing duplicates as i have multple rows for policy with same effective date. Also i have sorted the file for descending order in date due to which taking count less than 3. I need first 3 records for policy with different date. Example below:

0120210101 B
0120210101 A
0120210201 C
0120210301 C

O/p expected:

0120210101 B
0120210201 C
0120210301 C

Yes, you have ties in your data, and there are many options in PROC RANK on how to handle ties, most likely one of them will be to your liking.

 

Also, please don't post unintelligible dates like 0120210101 without an explanation. What is this?

--
Paige Miller
Sirisha1520
Calcite | Level 5
I was trying to explain this.

Policy Date Plan
01 20210101 B
01 20210101 A
01 20210201 C
01 20210301 C

O/p expected:

01 20210101 B
01 20210201 C
01 20210301 C
Kurt_Bremser
Super User

Let me help you formulate your question:

First, you want to have only max of Plan for any single date within a Policy.

Next, you want the earliest three dates for any given Policy.

Is that correct?

Kurt_Bremser
Super User

Then run this:

data have;
input Policy :$2. Date :yymmdd8. Plan :$1.;
format date yymmdd10.;
datalines;
01 20210101 B
01 20210101 A
01 20210201 C
01 20210301 C
;

proc sort data=have;
by policy date plan;
run;

data want;
set have;
by policy date;
if first.policy then count = 0;
if last.date;
count + 1; /* sum statement implies retain */
if count le 3;
drop count;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 12 replies
  • 1664 views
  • 0 likes
  • 4 in conversation