Solved

sas-delete records having count(*)>1

Posted on 2012-04-10
6
524 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Determine next b-weekly date 12 89
use lov values 2 50
How to create a unique rule restriction on a table for two fields 16 82
Mongo DB 18 42
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

773 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