BookmarkSubscribeRSS Feed
nataliavanessa0
Calcite | Level 5

Hi,

 

After sorting by date and ID, I am trying to select the first row per id that meets a certain criteria (x > 1), but if none of the rows (per id)  meet this criteria, I want to select the last row for that id?

 

I appreciate any help, thanks.

Natalia

1 REPLY 1
Reeza
Super User
data want;
set have;
by ID;
*hold value of flag across rows;
retain flag;

*initialize flag to 0 at start of each ID;
if first.id then flag=0;

*check if x >1 and flag is not 0 - has not happened before;
if x>1 and flag ne 1 then do;
    *set flag so you know it has been found;
   flag=1;
   *output record to dataset;
   output;

end;

*if last of the ID and flag is still 0 then output as well;
if last.id and flag=0 then output;

run;

@nataliavanessa0 wrote:

Hi,

 

After sorting by date and ID, I am trying to select the first row per id that meets a certain criteria (x > 1), but if none of the rows (per id)  meet this criteria, I want to select the last row for that id?

 

I appreciate any help, thanks.

Natalia


 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 378 views
  • 0 likes
  • 2 in conversation