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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 2 replies
  • 692 views
  • 3 likes
  • 3 in conversation