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

Hello, how do I return the second to the last observation when a condition is not met? I am using the last function to return observations that are within a certain date range. When the observation is not in a particular date range I want the record before it to be returned.

 

Here' an example. I want the last.id to be returned if the date range is le 12/31/2017 when this condition is not met i want it to return the 2nd to the last record for each id

 

ID   DATE

1   12/1/2016

1   11/3/2017

1   12/8/2019

2   12/1/2017

2   12/30/2017

3   12/1/2017

 

so ideally these records would be returned

ID   DATE

1    11/3/2017

2    12/30/2017

3    12/1/2017

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
   set have(where=(date<'31DEC17'd));
   by id;
   if last.id;
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
data want;
   set have(where=(date<'31DEC17'd));
   by id;
   if last.id;
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20

Sir @PaigeMiller  < or <= ? /* date range is le 12/31/2017*/

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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