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!!!

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!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 7 replies
  • 1436 views
  • 1 like
  • 2 in conversation