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*/

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 436 views
  • 3 likes
  • 3 in conversation