BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BenBrady
Obsidian | Level 7

I want to delete and keep observations based on multiple conditions. For example, in the following dataset I want to do the following:

If there is duplicates of a person i want to keep the one where score1=score2. If this still leave duplicates of that person then I want the person with the lower year (i.e 2015 not 2016). If there are duplicates for a person but score1 is not equal to score2 for any of them then I want to keep the one with the lower year. I have illustrated how i want the original data set to look below:

 

PersonScore1Score2Year
A15152015
A15152016
A15122016
B872015
C10102016
D11122015
D11132016
    
    
PersonScore1Score2Year
A15152015
B872015
C10102016
D11122015

 

Note that if there is no duplicates for a person then I want to keep them regardless of whether score1=score2.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

I hope the dataset has the same variables liek person, score1,score2 and year. if that is the case then replace the dataset name with your dataset, like replace have in the below code with your dataset name. this will create the dataset want.

 

proc sql;
create table want as select * from (select *, count(*) as count, 
case when count(*)>1 and score1=score2 then 1
when count(*)>1 and score1^=score2 then 2 else count(*) end as flag from have 
group by person,year ) group by person having min(year)=year and min(flag)=flag;
quit;
Thanks,
Jag

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

 

Pease try the sql

 

data have;
input Person$ Score1	Score2	Year;
cards;
A	15	15	2015
A	15	15	2016
A	15	12	2016
B	8	7	2015
C	10	10	2016
D	11	12	2015
D	11	13	2016
;

proc sql;
create table want as select * from (select *, count(*) as count,
case when count(*)>1 and score1=score2 then 1
when count(*)>1 and score1^=score2 then 2 else count(*) end as flag from have
group by person,year ) group by person having min(year)=year and min(flag)=flag;
quit;
Thanks,
Jag
BenBrady
Obsidian | Level 7


Thanks! How would I change the SAS code to make it applicable to an exisiting data set with the same variables that have thousands of observations?


Jagadishkatam
Amethyst | Level 16

I hope the dataset has the same variables liek person, score1,score2 and year. if that is the case then replace the dataset name with your dataset, like replace have in the below code with your dataset name. this will create the dataset want.

 

proc sql;
create table want as select * from (select *, count(*) as count, 
case when count(*)>1 and score1=score2 then 1
when count(*)>1 and score1^=score2 then 2 else count(*) end as flag from have 
group by person,year ) group by person having min(year)=year and min(flag)=flag;
quit;
Thanks,
Jag

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2128 views
  • 3 likes
  • 2 in conversation