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

Hi SAS experts,

I have a data in which there are many students and severals exams. If one student does not anwer all the questions, how do I eliminate him? For example:

data a;
input id$ answerkey$ response$ responsestatus$;
cards;
1 A B answered
1 C C answered
1 D D answered
1 B B answered
2 A B answered
2 C unanswered
2 D D answered
2 B B answered
3 A c answered
3 C C answered
3 D unanswered
3 B B answered
4 A C answered
4 C C answered
4 D C answered
4 B B answered
5 A unanswered
5 C unanswered
5 D unanswered
5 B unanswered
6 A B answered
6 C C answered
6 D c answered
6 B B answered
;
run;

 

How to get rid of id=5?

 

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
Rwon
Obsidian | Level 7

First off, the data set as written won't work as intended. The missing values for 'response' cause the variables to be read in wrong. There are many ways to fix this. I just manually added spaces to align the variables and updated the input statement.

 

data a;
input id$ answerkey$ response$ 5 responsestatus$ 7-16;
cards;
1 A B answered
1 C C answered
1 D D answered
1 B B answered
2 A B answered
2 C   unanswered
2 D D answered
2 B B answered
3 A C answered
3 C C answered
3 D   unanswered
3 B B answered
4 A C answered
4 C C answered
4 D C answered
4 B B answered
5 A   unanswered
5 C   unanswered
5 D   unanswered
5 B   unanswered
6 A B answered
6 C C answered
6 D c answered
6 B B answered
;
run;

*Creates a running total for the number of answers per ID then only outputs those with 4 answered questions and not ID of 5;
data b (drop=answer_flag answer_count);
set a;
by id;
if first.id then answer_count=0;

answer_flag=(responsestatus="answered"); 
answer_count+answer_flag;

if last.id then do;
  if answer_count = 4 and id ^= "5" then output;
  end;
run;

View solution in original post

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something like:

proc sql;
  delete from A where RESPONSE_STATUS="unanswered";
quit;

 

daisy6
Quartz | Level 8

As I understand, if I use the code you provided, it will eliminate  id=3 question3 since it is unanswered? 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ah, just reverse the logic then:

proc sql;
  create table TMP as select * from A where RESPONSE_STATUS="answered";
  delete from A where ID not in (select ID from TMP);
quit;

Ie if there is not an answered for that ID delete it. 

Cochetti
Fluorite | Level 6

This depends somewhat on how your data are formatted.  If the "answered/unanswered" variables are stored as 0/1, it could be just as  easy as summing the responses and using a WHERE condition:

 

TotalResp = A + B + C + D + E

... 

WHERE TotalResp = 5

Otherwise, you could compute the number of responses missing this way using the missing function. Then, exclude those for which all are blank:

Total_Blank_Resp = missing( A ) + missing( B ) + missing( C ) + missing( D ) + missing( E )

...

WHERE Total_Blank_Resp = 5

 

Ksharp
Super User
data a;
input id$ answerkey$ response$ responsestatus : $20.;
cards;
1 A B answered
1 C C answered
1 D D answered
1 B B answered
2 A B answered
2 C . unanswered
2 D D answered
2 B B answered
3 A c answered
3 C C answered
3 D . unanswered
3 B B answered
4 A C answered
4 C C answered
4 D C answered
4 B B answered
5 A . unanswered
5 C . unanswered
5 D . unanswered
5 B . unanswered
6 A B answered
6 C C answered
6 D c answered
6 B B answered
;
run;
proc sql;
select *
 from a 
  group by id 
   having sum(responsestatus='unanswered') ne count(*);
quit;
daisy6
Quartz | Level 8

Ksharp,

It works! Thank you very much!

Rwon
Obsidian | Level 7

First off, the data set as written won't work as intended. The missing values for 'response' cause the variables to be read in wrong. There are many ways to fix this. I just manually added spaces to align the variables and updated the input statement.

 

data a;
input id$ answerkey$ response$ 5 responsestatus$ 7-16;
cards;
1 A B answered
1 C C answered
1 D D answered
1 B B answered
2 A B answered
2 C   unanswered
2 D D answered
2 B B answered
3 A C answered
3 C C answered
3 D   unanswered
3 B B answered
4 A C answered
4 C C answered
4 D C answered
4 B B answered
5 A   unanswered
5 C   unanswered
5 D   unanswered
5 B   unanswered
6 A B answered
6 C C answered
6 D c answered
6 B B answered
;
run;

*Creates a running total for the number of answers per ID then only outputs those with 4 answered questions and not ID of 5;
data b (drop=answer_flag answer_count);
set a;
by id;
if first.id then answer_count=0;

answer_flag=(responsestatus="answered"); 
answer_count+answer_flag;

if last.id then do;
  if answer_count = 4 and id ^= "5" then output;
  end;
run;
daisy6
Quartz | Level 8
Thank all of you for the help!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 8 replies
  • 1254 views
  • 2 likes
  • 5 in conversation