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

Hi,

I need to make a filter keeping only all the last rows marking "Flag=1".

Reading from bottom to top, I want to keep only the results until I get a value other than 1 for the variable "Flag".

Here's an example in the table below.

 

Input table

IDFlagSub
10002NA
10001AB
10009UH
10001IF
10005GE
10001IP
10001AB
10001EH
10001IA

 

Output table

IDFlagSub
10001IP
10001AB
10001EH
10001IA

 

I need to have a SAS code that does this, could anyone help me?

Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input ID	Flag	Sub $;
cards;
1000	2	NA
1000	1	AB
1000	9	UH
1000	1	IF
1000	5	GE
1000	1	IP
1000	1	AB
1000	1	EH
1000	1	IA
2000	5	NA
2000	6	AB
2000	7	UH
2000	1	IF
2000	10	GE
2000	1	ES
2000	1	SW
2000	1	GF
2000	1	DY
2000	1	ZX
;

data want;
call missing(c);
do until(last.id);
 set have;
 by id flag notsorted;
 if first.flag then c+1;
end;
call missing(n);
do until(last.id);
 set have;
 by id flag notsorted;
 if first.flag then n+1;
 if n=c and flag=1 then output;
end;
drop n c;
run;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

data have;
input ID	Flag	Sub $;
cards;
1000	2	NA
1000	1	AB
1000	9	UH
1000	1	IF
1000	5	GE
1000	1	IP
1000	1	AB
1000	1	EH
1000	1	IA
;

data t;
do n=nobs to 1 by -1;
set have point=n nobs=nobs;
_n=n;
if flag=1 then output;
else stop;
end;
run;

proc sort data=t out=want(drop=_n);
by _n;
run;


usuario_estudo
Calcite | Level 5

Thank you very much, that was exactly it.

Could you tell me how to change the code if I want to do the same for each different IDs?

 

Example:

 

 

Input table

IDFlagSub
10002NA
10001AB
10009UH
10001IF
10005GE
10001IP
10001AB
10001EH
10001IA
20005NA
20006AB
20007UH
20001IF
200010GE
20001ES
20001SW
20001GF
20001DY
20001ZX

 

Output table

IDFlagSub
10001IP
10001AB
10001EH
10001IA
20001ES
20001SW
20001GF
20001DY
20001ZX
novinosrin
Tourmaline | Level 20

Sorry my bad, i didn;t see ID earlier. brb

 

Also @usuario_estudo Kindly revert the question back to open i.e not accepted coz it was my mistake 

usuario_estudo
Calcite | Level 5

Ok, I did that.

Thank you very much!

 

novinosrin
Tourmaline | Level 20
data have;
input ID	Flag	Sub $;
cards;
1000	2	NA
1000	1	AB
1000	9	UH
1000	1	IF
1000	5	GE
1000	1	IP
1000	1	AB
1000	1	EH
1000	1	IA
2000	5	NA
2000	6	AB
2000	7	UH
2000	1	IF
2000	10	GE
2000	1	ES
2000	1	SW
2000	1	GF
2000	1	DY
2000	1	ZX
;

data want;
call missing(c);
do until(last.id);
 set have;
 by id flag notsorted;
 if first.flag then c+1;
end;
call missing(n);
do until(last.id);
 set have;
 by id flag notsorted;
 if first.flag then n+1;
 if n=c and flag=1 then output;
end;
drop n c;
run;
novinosrin
Tourmaline | Level 20

I did a small edit to include call missing, my apologies for not reading the question/dataset earlier. See if the above works. Thanks!

usuario_estudo
Calcite | Level 5

Yes, it's perfect, thank you!!!

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2662 views
  • 1 like
  • 2 in conversation