BookmarkSubscribeRSS Feed
usuario_estudo
Calcite | Level 5

Hi,

I need help writing a code on Sas Enterprise Guide.

 

I need to make a filter keeping only the last rows marking "Flag=1" for each ID number.

As shown in the table below in blue:

 

IDFlag
10002
10001
10009
10001
10005
10001
10001
10001
10001
200011
200012
20004
20001
20001
20003
20001
20001
20001

 

Thank you very much!

3 REPLIES 3
V_27
Obsidian | Level 7

 

 

Hi @usuario_estudo,

 

I didn't followed the question fully but here is an attempt.

 

data have;
input ID Flag;
datalines;
1000	2
1000	1
1000	9
1000	1
1000	5
1000	1
1000	1
1000	1
1000	1
2000	11
2000	12
2000	4
2000	1
2000	1
2000	3
2000	1
2000	1
2000	1
;
run;
proc sort data=have out=new;
by ID descending Flag;
run;
data want;
set new;
by ID;
if last.ID then output;
run;
usuario_estudo
Calcite | Level 5

Thank you for the code, but it does not return the desired result.

I would like to return the following table:

 

IDFlag
10001
10001
10001
10001
20001
20001
20001


Thank you very much! 

Ksharp
Super User
data have;
input ID Flag;
datalines;
1000	2
1000	1
1000	9
1000	1
1000	5
1000	1
1000	1
1000	1
1000	1
2000	11
2000	12
2000	4
2000	1
2000	1
2000	3
2000	1
2000	1
2000	1
;
run;
data want;
 do until(last.flag);
  set have;
  by id flag notsorted;
  if last.id then  found=1;
 end;
  do until(last.flag);
  set have;
  by id flag notsorted;
  if found and flag=1 then output;
 end;
 drop found;
 run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1197 views
  • 0 likes
  • 3 in conversation