Solved

sas-delete records having count(*)>1

Posted on 2012-04-10
6
495 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now