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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 352 views
  • 1 like
  • 2 in conversation