Solved

sas-delete records having count(*)>1

Posted on 2012-04-10
6
540 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

829 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