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

Hello,

I have following dataset. (There are other variables too but this sample dataset has only two variables)

IDvariable1
1NA
2C
2NC
3NA
3C
3NC
4NA
4NA
4NA
4NA
5NC
5NC
5NC

 

I would like to subset into two dataset where dataset 1 is: (This data will contains all rows for an ID if the value for variable 1 is NA.)

IDvariable1
1NA
4NA
4NA
4NA
4NA

 

and Dataset 2 is:

IDvariable1
2C
2NC
3NA
3C
3NC
5NC
5NC
5NC

 

Would appreciate your help with this.

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here is one way

 

data have;
input ID variable1 $;
datalines;
1 NA
2 C
2 NC
3 NA
3 C
3 NC
4 NA
4 NA
4 NA
4 NA
5 NC
5 NC
5 NC
;

proc sql;
    create table one as
    select * from have
    group by ID
    having n(variable1)=sum(variable1='NA');
    create table two as
    select * from have
    group by ID
    having n(variable1) ne sum(variable1='NA');
quit;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Here is one way

 

data have;
input ID variable1 $;
datalines;
1 NA
2 C
2 NC
3 NA
3 C
3 NC
4 NA
4 NA
4 NA
4 NA
5 NC
5 NC
5 NC
;

proc sql;
    create table one as
    select * from have
    group by ID
    having n(variable1)=sum(variable1='NA');
    create table two as
    select * from have
    group by ID
    having n(variable1) ne sum(variable1='NA');
quit;

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