Solved

sas-delete records having count(*)>1

Posted on 2012-04-10
6
557 Views
Last Modified: 2012-04-20
Hi,
i am getting count(*)>1 while running below query so i want to delete dups
and just keep count(*)=1 how can i do that

select item_id||':'||put(date,best.) from table
group by item_id||':'||put(date,best.)
having count(*)>1
0
Comment
Question by:sam2929
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 37831115
assuming item_id||':'||put(date,best.) provides the unique identifier to the records to be deleted:

delete from table
where item_id||':'||put(date,best.) in (
    select item_id||':'||put(date,best.)
    from table
    group by item_id||':'||put(date,best.)
    having count(*)>1)

have you tried this?
0
 
LVL 8

Expert Comment

by:ShannonEE
ID: 37831143
Hi sam2929,
proc sort data=TABLE out=NEW_TABLE nodupkey;
by item_id;
run;

Open in new window


You may want to also check if "noduprecs" may fit your purpose better than  "nodupkey".
See  http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000146878.htm
for more information about sort options.

Ian.
0
 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 500 total points
ID: 37831178
Hi

Alternatively, just instruct SQl to select UNIQUE or DISTINCT records on the select statement:

 PROC SQL;
  SELECT DISTINCT item_id||':'||put(date,best.) from table ;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ShannonEE
ID: 37831189
proc sort data=TABLE out=NEW_TABLE nodupkey;
by item_id date;
run;

Open in new window


May be required if date field is critical in checking for duplicates.
0
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 37831252
reading the question, asker would like to delete records from table where record count is greater than 1, and just keep records where record count is 1, in which case, would be my solution.

however, if asker meant to ask how to delete the duplicates, such that for each and every item_id and date field, only 1 record is left as the end state, then ShannonEE's post above would be right
0
 
LVL 8

Expert Comment

by:ShannonEE
ID: 37831355
so i want to delete dups

I assumed this to mean keep one representative and delete the remaining DUPLICATES though there is a worrying notion about removing information and keeping an arbitrary representative.

 Using a data step it is trivial to keep the first, the last, or in fact the 2nd or 2nd last, or other possibilities in any possible sorting order (using other variables after item_id and date).

More complicated but still possible is to keep the middle record in each group - requires 2 sorts, or to be exact 1 sort and a reversal.


The question as posed wants to delete duplicate records where the duplication is only on the two fields item_id and date, however this throws away more information than the case of deleting duplicates where the duplication is on all fields.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question