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

Hi Friends - i have one SAS dataset with around 1/2 millions of records with some duplicate records on field SKU. I want to know which SKU has maximum duplicate record itself and which has minimum duplicate records for same field, SKU?

Can someone please tell me how can i do this?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
stataddict
Calcite | Level 5

proc sql;

create table max_min as

select SKU, count(SKU) as duplicates

from your_table

group by SKU

having duplicates > 1

order by duplicates desc

;

quit;

You will get only duplicate values, unique ones will not show up.

First record will be SKU with maximum duplicates.

View solution in original post

4 REPLIES 4
stataddict
Calcite | Level 5

proc sql;

create table max_min as

select SKU, count(SKU) as duplicates

from your_table

group by SKU

having duplicates > 1

order by duplicates desc

;

quit;

You will get only duplicate values, unique ones will not show up.

First record will be SKU with maximum duplicates.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Updated slightly, with this you can select any number of min/max values, in this instance I want bottom and top 2 records:

proc sql;

  create table WORK.WANT as

  select  *

  from    (

            select  TYPE,

                    COUNT(TYPE) as COUNT_OF_TYPE

            from    SASHELP.CARS

            group by TYPE

          )

  order by COUNT_OF_TYPE;

  select  COUNT(TYPE)

  into    :NUM_OBS

  from    WORK.WANT;

quit;

data want;

  set want;

  if _n_ <= 2 or _n_ > (&NUM_OBS. - 2) then output;

run;

TomKari
Onyx | Level 15

The query in the first response is also doable in the "Query Builder" in exactly the same way, and you can then post-process it with additional queries, the SORT task, and the RANK task.

Tom

jimksas
Calcite | Level 5

Thanks Stataddict, RW9 and Tom.

Everything works grt...!!!

I also did validate number of observation using PROC REPORT.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 4 replies
  • 2630 views
  • 6 likes
  • 4 in conversation