BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aabbccwyt
Obsidian | Level 7
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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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
;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
aabbccwyt
Obsidian | Level 7

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

Screen Shot 2021-10-19 at 1.24.09 AM.png

Reeza
Super User

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

Screen Shot 2021-10-19 at 1.24.09 AM.png


 

PaigeMiller
Diamond | Level 26

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
;
--
Paige Miller

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
  • 4 replies
  • 599 views
  • 3 likes
  • 3 in conversation