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

I'm trying to do the following:

  • If a quarter occurs more then once in a type I want to keep only the quarter with the highest phase. The other ones need to be deleted. 

This is the data I have (in reality much bigger!):

data have;
infile datalines;
input type $ quarter phase;
datalines;
K-11 202001 1
K-11 202101 2
K-11 202003
K-11 202004 3
K-12 202101 3
K-12 202102 1
K-12 202103 1
K-12 202103 1
K-13 202003 2
K-13 202103 3
K-13 202103 2
K-13 202103 2
;

This is the data I want:

K-11 202001 1
K-11 202101 2
K-11 202003
K-11 202004 3
K-12 202101 3
K-12 202102 1
K-12 202103 1
K-13 202003 2
K-13 202103 3
;



1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Here you go.

data have;
  infile datalines truncover;
  input type $ quarter phase;
  datalines;
K-11 202001 1
K-11 202101 2
K-11 202003
K-11 202004 3
K-12 202101 3
K-12 202102 1
K-12 202103 1
K-12 202103 1
K-13 202003 2
K-13 202103 3
K-13 202103 2
K-13 202103 2
;

proc sort data=have out=want;
  by type quarter descending phase;
run;

data want;
  set want;
  by type quarter;

  if first.quarter;
run;

proc print data=want;
run;

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

Here you go.

data have;
  infile datalines truncover;
  input type $ quarter phase;
  datalines;
K-11 202001 1
K-11 202101 2
K-11 202003
K-11 202004 3
K-12 202101 3
K-12 202102 1
K-12 202103 1
K-12 202103 1
K-13 202003 2
K-13 202103 3
K-13 202103 2
K-13 202103 2
;

proc sort data=have out=want;
  by type quarter descending phase;
run;

data want;
  set want;
  by type quarter;

  if first.quarter;
run;

proc print data=want;
run;
Patrick
Opal | Level 21

Or here a 2nd option to achieve the same. Bit special but it has the advantage that SAS "knows" that want is sorted which can be beneficial for performance for downstream processing requiring data sorted this way.

data have;
  infile datalines truncover;
  input type $ quarter phase;
  datalines;
K-11 202001 1
K-11 202101 2
K-11 202003
K-11 202004 3
K-12 202101 3
K-12 202102 1
K-12 202103 1
K-12 202103 1
K-13 202003 2
K-13 202103 3
K-13 202103 2
K-13 202103 2
;

proc sort data=have out=want;
  by type quarter descending phase;
run;

proc sort data=want out=want nodupkey;
  by type quarter;
run;

proc print data=want;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 829 views
  • 1 like
  • 2 in conversation