- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 04-04-2011 12:58 AM
(13767 views)
Hi there
Firstly apologies if this question is in the wrong place.
Secondly, more apologies for being such a newbie and having to ask about such basic things!
and finally.....
A bit of a theoretical question for you all.
How would you go about deleting all duplicate observations from a dataset??
eg: I have two datasets containing the same information, downloaded a month apart. The latest dataset contains all the observations which appear in the earlier set plus a few dozen new observations.
I know NODUP and NODUPKEY only write the first occurrence of a duplicate to the set you’re creating. That’s great, but I want to remove both occurrences of the duplicates, leaving me with a dataset that contains only the new observations.
Any useful tips would be much appreciated.
Thanks in advance
Firstly apologies if this question is in the wrong place.
Secondly, more apologies for being such a newbie and having to ask about such basic things!
and finally.....
A bit of a theoretical question for you all.
How would you go about deleting all duplicate observations from a dataset??
eg: I have two datasets containing the same information, downloaded a month apart. The latest dataset contains all the observations which appear in the earlier set plus a few dozen new observations.
I know NODUP and NODUPKEY only write the first occurrence of a duplicate to the set you’re creating. That’s great, but I want to remove both occurrences of the duplicates, leaving me with a dataset that contains only the new observations.
Any useful tips would be much appreciated.
Thanks in advance
14 REPLIES 14
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
proc sql;
create table tabB as
select obs from tabA
group by obs having count(*)>1;
delete from tabA where obs in (select obs from tabB);
quit;
create table tabB as
select obs from tabA
group by obs having count(*)>1;
delete from tabA where obs in (select obs from tabB);
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I wonder if it achieves the intended goal. Adre the remaing obs all new ones? I'd like to know how to delete extra dup using SQL. Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
How would you distinguish "..only the new observation"? If there is some event-date/time variable, you can sort the file with your "base" BY variable list along with DESCENDING -- but don't code NODUPKEY in the first sort.
Then issue a second SORT using NODUPKEY along with the EQUALS parameter while only providing your "base" BY variable list.
The other alternative using a DATA step is to ensure that your input file gets sorted into the desired sequence, then use a DATA step with a SET and a BY statement with your "base" BY variable list. And in an IF/THEN OUTPUT; statement, use the LAST. technique so that only the last-occurrence of any "by variable group" of observations (presumably the newest obs?) will be output.
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search arguments, this topic / post:
proc sort nodupkey site:sas.com
data step by group processing site:sas.com
data step remove duplicates site:sas.com
Then issue a second SORT using NODUPKEY along with the EQUALS parameter while only providing your "base" BY variable list.
The other alternative using a DATA step is to ensure that your input file gets sorted into the desired sequence, then use a DATA step with a SET and a BY statement with your "base" BY variable list. And in an IF
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search arguments, this topic / post:
proc sort nodupkey site:sas.com
data step by group processing site:sas.com
data step remove duplicates site:sas.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I think people might be overthinking this. How about:
[pre]
proc sql;
select * from new
except
select * from old;
[/pre]
[pre]
proc sql;
select * from new
except
select * from old;
[/pre]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
To RickM: How would the PROC SQL example address the OP's stated objective with removing all duplicates within a given SAS file, while only retaining the "most recent" observation for a given "by variable list"?
Scott Barry
SBBWorks, Inc.
Scott Barry
SBBWorks, Inc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
sbb,
From the OP:
"eg: I have two datasets containing the same information, downloaded a month apart. The latest dataset contains all the observations which appear in the earlier set plus a few dozen new observations. "
So the problem, as I interpreted it, is that they are starting out with two data sets. The other solutions assume the data is already combined and removing duplicates within one dataset is the only way to solve the problem. Why not find the new values before the two datasets are put together.
From the OP:
"eg: I have two datasets containing the same information, downloaded a month apart. The latest dataset contains all the observations which appear in the earlier set plus a few dozen new observations. "
So the problem, as I interpreted it, is that they are starting out with two data sets. The other solutions assume the data is already combined and removing duplicates within one dataset is the only way to solve the problem. Why not find the new values before the two datasets are put together.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
[pre]
proc sort data=class ;
by name sex weight height;
run;
data class;
set class;
by name sex weight height;
if not (first.height and last.height) then delete;
run;
[/pre]
Ksharp
proc sort data=class ;
by name sex weight height;
run;
data class;
set class;
by name sex weight height;
if not (first.height and last.height) then delete;
run;
[/pre]
Ksharp
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is there a unique identifier in each record? If so....
proc sort data=one;by ID;run;
proc sort data=two;by ID;run;
data match;
merge one (in=a) two (in=b);
by ID;
if a and b then from='M';
if a and not b then from='A';
if not a and b then from='B';
run;
proc freq data=match;
table from;
run;
*/the observations where from='B' are in the 2nd set only*/
data new;
set match;
if from ne 'B' then delete;
run;
proc sort data=one;by ID;run;
proc sort data=two;by ID;run;
data match;
merge one (in=a) two (in=b);
by ID;
if a and b then from='M';
if a and not b then from='A';
if not a and b then from='B';
run;
proc freq data=match;
table from;
run;
*/the observations where from='B' are in the 2nd set only*/
data new;
set match;
if from ne 'B' then delete;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi there and thanks for all your help,
I probably didn't explain things as clearly as I could, so to clear things up, my problem was two data sets ('this month' and 'last month') with identical variables (although the values of one variable 'annualpremium' may differ from month to month). 'last month' has an unidentified amount of observations which have been dropped and are not contained in 'this month'.
I needed to produce one dataset containing only the dropped observations, ie: those that appear in 'last month' that don't appear in 'this month'. No other criteria at all.
This is how I managed it in the end, probably not the most efficient method but it seems to have generated the right results........
proc sql;
create table combined as
select t1.PolicyNumber,
t1.OriginalStartDate,
t1.AnnualPremium,
t2.PolicyNumber as PolicyNumber2
from thismonth as t1
left join
lastmonth as t2
on t1.PolicyNumber = t2.PolicyNumber;
quit;
data droppedpolicy (keep = PolicyNumber OriginalStartDate AnnualPremium);
set combined;
if PolicyNumber = PolicyNumber2 then delete;
run;
I probably didn't explain things as clearly as I could, so to clear things up, my problem was two data sets ('this month' and 'last month') with identical variables (although the values of one variable 'annualpremium' may differ from month to month). 'last month' has an unidentified amount of observations which have been dropped and are not contained in 'this month'.
I needed to produce one dataset containing only the dropped observations, ie: those that appear in 'last month' that don't appear in 'this month'. No other criteria at all.
This is how I managed it in the end, probably not the most efficient method but it seems to have generated the right results........
proc sql;
create table combined as
select t1.PolicyNumber,
t1.OriginalStartDate,
t1.AnnualPremium,
t2.PolicyNumber as PolicyNumber2
from thismonth as t1
left join
lastmonth as t2
on t1.PolicyNumber = t2.PolicyNumber;
quit;
data droppedpolicy (keep = PolicyNumber OriginalStartDate AnnualPremium);
set combined;
if PolicyNumber = PolicyNumber2 then delete;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
how about:
proc sql;
create table DroppedRecs as
select
t1.*
from
lastmonth t1
left join thismonth t2
on t1.policynumber=t2.policynumber
where
t2.policynumber is null;
quit;
proc sql;
create table DroppedRecs as
select
t1.*
from
lastmonth t1
left join thismonth t2
on t1.policynumber=t2.policynumber
where
t2.policynumber is null;
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi
Proc sort data=datasetname noduprec out=newdatasetname;
by varname1 varname2;
run;
Proc sort data=datasetname noduprec out=newdatasetname;
by varname1 varname2;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
My suggestion is to watch Cynthia's video on the very same problem where she suggests using NODUPKEY
this is the URL:
http://www.youtube.com/watch?v=Srs7VhEqBts
My suggestion is to watch Cynthia's video on the very same problem where she suggests using NODUPKEY
this is the URL:
http://www.youtube.com/watch?v=Srs7VhEqBts
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Her video was on the basic proc sort. It did not address what the OP was asking.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I realize this is an old thread and the op already came up with a solution, but for future reference and my own development would the "distinct" keyword not have worked to remove all observations that were identical across all variables in proc sql? For instance,
proc sql;
create table x as
select DISTINCT (variable1),
variable2,
variable3,
variable4,
etc....
from old_table;
quit;