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

I want to select records from an existing dataset based on this logic: going through each record from top to bottom, locate the record with value 0 such that all ensuing records always have value of 0, select that record and everything before that record. In the following dataset, I'm making such selection for each type value, and my desired results are records with id 4,5,8,9,10. How do I achieve this? Thanks.

 

id   type   value

1    A        1

2    A        0

3    A        1

4    A        0

5    A        0

6    B        0

7    B        1

8    B        0

9    B        0

10  B        0

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input id   type $  value;
cards;
1    A        1
2    A        0
3    A        1
4    A        0
5    A        0
6    B        0
7    B        1
8    B        0
9    B        0
10  B        0
;
proc sql;
create table want(drop=_:) as
select * 
from (select *,value=1 as _v, max(id) as _m from have group by type,_v)
group by type
having id>max((_v=1)*_m)
order by id, type;
quit;

View solution in original post

5 REPLIES 5
Astounding
PROC Star

One approach:

 

data want;

start_here = 0;

recnum = 0;

do until (last.id);

   set have;

   by id value notsorted;

   recnum + 1;

   if value ne 0 then start_here = 0;

   else if first.value then start_here = recnum;

end;

recnum = 0;

do until (last.id);

   set have;

   by id;

   recnum + 1;

   if start_here > 0 and recnum >= start_here then output;

end;

drop recnum start_here;

run;

 

It's untested code, but looks like it should work.

novinosrin
Tourmaline | Level 20

same idea, another variant:

 

data have;
input id   type $  value;
cards;
1    A        1
2    A        0
3    A        1
4    A        0
5    A        0
6    B        0
7    B        1
8    B        0
9    B        0
10  B        0
;

data want;
_k=.;
do _n=1 by 1 until(last.type);
set have;
by type;
if value then _k=_n;
end;
do _n_=1 by 1 until(last.type);
set have;
by type;
if  _n_>_k then output;
end;
drop _:;
run;
novinosrin
Tourmaline | Level 20
data have;
input id   type $  value;
cards;
1    A        1
2    A        0
3    A        1
4    A        0
5    A        0
6    B        0
7    B        1
8    B        0
9    B        0
10  B        0
;
proc sql;
create table want(drop=_:) as
select * 
from (select *,value=1 as _v, max(id) as _m from have group by type,_v)
group by type
having id>max((_v=1)*_m)
order by id, type;
quit;
Ksharp
Super User

If I understood what you mean.

 

data have;
input id   type $  value;
cards;
1    A        1
2    A        0
3    A        1
4    A        0
5    A        0
6    B        0
7    B        1
8    B        0
9    B        0
10  B        0
;
run;

data temp;
 set have;
 by type value notsorted;
 group+first.value;
 if value=0;
run;
data want;
 set temp;
 by type group;
 if first.type then _group=0;
 _group+first.group;
 if _group ne 1 ;
 drop group _group;
run;
apolitical
Obsidian | Level 7

I'm sure all suggested answers work fine. I chose the one I could understand the most easily as the answer. Thank you all.

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
  • 5 replies
  • 1358 views
  • 1 like
  • 4 in conversation