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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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