BookmarkSubscribeRSS Feed
d0816
Quartz | Level 8

Hello,

I want to subset a dataset into two datasets where in one dataset I want to keep all rows for an ID if the value for another variable is a certain value.

For eg.

Dataset I have:

IDclassstatus
11complete
12Incomplete
13complete
21Incomplete
22Incomplete
23Incomplete
31Incomplete
32Incomplete
41complete
42complete
43complete
44complete

 

 

Datasets I want are:

Dataset 1: status is all "Incomplete" for an ID.

IDclassstatus
21Incomplete
22Incomplete
23Incomplete
31Incomplete
32Incomplete

 

Dataset 2: status is all "Complete" or combination of "Complete/ Incomplete" for an ID.

IDclassstatus
11complete
12Incomplete
13complete
41complete
42complete
43complete
44complete

 

 

Thank you for your help.

 

 

2 REPLIES 2
novinosrin
Tourmaline | Level 20

If easy and lazy solutions are acceptable, here is one:

 

data have;
input ID	class	status $12.;
cards;
1	1	complete
1	2	Incomplete
1	3	complete
2	1	Incomplete
2	2	Incomplete
2	3	Incomplete
3	1	Incomplete
3	2	Incomplete
4	1	complete
4	2	complete
4	3	complete
4	4	complete
;

proc sql;
create table one as
select id, class, status
from have
group by id
having sum(status='Incomplete')=n(id)
order by id, class;
quit;

proc sql;
create table two as
select id, class, status
from have
group by id
having sum(status='complete')>0
order by id, class;
quit;
PGStats
Opal | Level 21

Simple and fast with a data step:

 

data ds1 ds2;
do until(last.ID);
    set have; by id;
    if status ne "Incomplete" then found = 1;
    end;
do until(last.ID);
    set have; by id;
    if found then output ds2;
    else output ds1;
    end;
drop found;
run;
PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1097 views
  • 3 likes
  • 3 in conversation