I have a data set which is much larger than my example below. I want to be able to remove the 'Person' duplicates by the 'type' variable. In particular, if they have the same 'Type', then can keep either. However, if they have different values for 'Type', i want to keep the record with the higher frequency. In this case it would be one with Type=2 (as there is 2 of them compared to only one record where Type=1). How would I go about doing this?
Person | Type |
A | 2 |
A | 2 |
A | 1 |
B | 3 |
B | 3 |
Try this.
proc sql;
create table want1 as
select *, count(*) as n from have group by person, type order by person, calculated n desc;
run;
data want;
set want1;
by person descending n;
if first.person;
drop n;
run;
like this?
data have;
input Person$ Type;
datalines;
A 2
A 2
A 1
B 3
B 3
;
proc sort data=have;
by descending Person descending Type;
run;
data want;
set have;
by descending Person descending Type;
if first.Person;
run;
Here is one way to do it, using SQL:
proc sql;
create table want as select * from
(select * from
(select *,count(*) as n from have
group by person,type
)
group by person
having n=max(n)
)
group by person
having type=min(type);
quit;
Try this.
proc sql;
create table want1 as
select *, count(*) as n from have group by person, type order by person, calculated n desc;
run;
data want;
set want1;
by person descending n;
if first.person;
drop n;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.