data stan;
array Key[20] $ _temporary_ ('A','B','C','D','C','B','A','D','B','A','A','C','B','D','D','B','A','A','B','B');
array Question[20] $;
input Student_ID $ Gender $ Question[*];
Scores = 0;
do i = 1 to 20;
if Question[i] eq Key[i] then Scores = 1;
end;
drop i;
datalines;
A1 Female A B A D B C D A A A C D B C A C C B D A
A2 Female B C D A A A B C D A D C D B D A A D C D
A3 Male C C C A A B D B D B C D C D C A A D A A
A4 Male A D A C C C B D C A B A C A D D A B A C
;
proc print data = stan;
title "total correct questions by item";
var Student_ID Scores: ;
run;
I want to score a test for 4 people by using a temporary array. The outputs should show each grade for each student, indicated by a 1 or a 0. However, my current results are completely wrong. I think my problem comes from the proc print part but i don't know where it is wrong. Any help? Thanks in advance!
You need an ARRAY to store the 20 scores.
data stan;
array Key[20] $ _temporary_ ('A','B','C','D','C','B','A','D','B','A','A','C','B','D','D','B','A','A','B','B');
array scores[20];
array Question[20] $;
input Student_ID $ Gender $ Question[*];
do i = 1 to 20;
Scores[i] = (Question[i] eq Key[i]);
end;
drop i;
datalines;
A1 Female A B A D B C D A A A C D B C A C C B D A
A2 Female B C D A A A B C D A D C D B D A A D C D
A3 Male C C C A A B D B D B C D C D C A A D A A
A4 Male A D A C C C B D C A B A C A D D A B A C
;
Wrong because you get only one value in variable SCORES for each student when you are expecting 20 values?
Or wrong because you want the sum of the 20 zero/one values?
Or wrong for some other reason?
Hi,
It is wrong because I'm getting only 1 value for each student when I'm expecting 20 values.
To be more specific, here is what I have
You don't have 20 score values so why would you expect 20 values, you only have 1 score value.
If you want 20 you need to create another array to hold those twenty values.
FYI - IMO this is easier if you transpose and merge rather than an array. Easier to summarize by question later on too to see which questions are harder than others.
@aabbccwyt wrote:
Hi,
It is wrong because I'm getting only 1 value for each student when I'm expecting 20 values.
To be more specific, here is what I have
You need an ARRAY to store the 20 scores.
data stan;
array Key[20] $ _temporary_ ('A','B','C','D','C','B','A','D','B','A','A','C','B','D','D','B','A','A','B','B');
array scores[20];
array Question[20] $;
input Student_ID $ Gender $ Question[*];
do i = 1 to 20;
Scores[i] = (Question[i] eq Key[i]);
end;
drop i;
datalines;
A1 Female A B A D B C D A A A C D B C A C C B D A
A2 Female B C D A A A B C D A D C D B D A A D C D
A3 Male C C C A A B D B D B C D C D C A A D A A
A4 Male A D A C C C B D C A B A C A D D A B A C
;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.