The ascii data file called grades.dat contains five exam scores (Score1, Score2, ..., Score5) for each of four students in a class. The first column contains the student id number (ID). The passing grade on each of the five exams was determined to be 65, 70, 60, 75, and 66, respectively. Your job is to read the ascii data into a temporary SAS data set called answers. Then, determine the number of exams passed by each student (Passed). In doing so: you should take advantage of arrays wherever possible you should take advantage of shortcut lists, such as numbered range lists, named range lists, or special name lists, wherever possible you should use temporary array elements wherever possible you should use iterative DO loops wherever possible When all is said and done, your answers data set should contain seven variables: ID (student id number), the five exam grades (Score1, Score2, ..., Score5), and the number of exams each student passed (Passed). Print the resulting answers data set.
grades.dat datalines are :
001 50 70 62 78 85
002 90 86 87 91 94
003 63 72 58 73 68
004 60 68 59 70 65
Thanks for your reply,
Hi,
Sorry, your question is not clear. Post test data (form of a datastep). And what the output should look like. It looks like from what you have posted there is that the output statement appears before the assignment of passed in:
output;
passedin=x+y+z+a+b;
end;
Replace with:
passedin=x+y+z+a+b;
output;
end;
Also, not sure what your code is trying to do (without test data and required output), but why not just:
data want; set score; array score(5) score1-score5; passedin=0; if score(1) ge 65 then passedin=sum(passedin,1); if score(2) ge 70 then passedin=sum(passedin,1); if score(3) ge 60 then passedin=sum(passedin,1); if score(4) ge 75 then passedin=sum(passedin,1); if score(5) ge 66 then passedin=sum(passedin,1); output; run;
We can't help without your input dataset.
Please supply the contents of work.score in a data step with cards;
Hi,
Sorry, your question is not clear. Post test data (form of a datastep). And what the output should look like. It looks like from what you have posted there is that the output statement appears before the assignment of passed in:
output;
passedin=x+y+z+a+b;
end;
Replace with:
passedin=x+y+z+a+b;
output;
end;
Also, not sure what your code is trying to do (without test data and required output), but why not just:
data want; set score; array score(5) score1-score5; passedin=0; if score(1) ge 65 then passedin=sum(passedin,1); if score(2) ge 70 then passedin=sum(passedin,1); if score(3) ge 60 then passedin=sum(passedin,1); if score(4) ge 75 then passedin=sum(passedin,1); if score(5) ge 66 then passedin=sum(passedin,1); output; run;
To answer your original question, using ELSE is incorrect. Your program must examine all the scores, and ELSE prevents that once the first true comparison has been found.
There are other errors as well. RETAIN is incorrect. There is no reason that X should remain 1 forever, once a single SCORE1 is passing.
RW9 has posted a good approach. I just wanted to make sure that your original question got answered.
The ascii data file called grades.dat contains five exam scores (Score1, Score2, ..., Score5) for each of four students in a class. The first column contains the student id number (ID). The passing grade on each of the five exams was determined to be 65, 70, 60, 75, and 66, respectively. Your job is to read the ascii data into a temporary SAS data set called answers. Then, determine the number of exams passed by each student (Passed). In doing so: you should take advantage of arrays wherever possible you should take advantage of shortcut lists, such as numbered range lists, named range lists, or special name lists, wherever possible you should use temporary array elements wherever possible you should use iterative DO loops wherever possible When all is said and done, your answers data set should contain seven variables: ID (student id number), the five exam grades (Score1, Score2, ..., Score5), and the number of exams each student passed (Passed). Print the resulting answers data set.
grades.dat datalines are :
001 50 70 62 78 85
002 90 86 87 91 94
003 63 72 58 73 68
004 60 68 59 70 65
Thanks for your reply,
Maximizing use fo arrays and do loop per the instructions i get something like this:
data want;
set score;
array score(5) score1-score5;
array passing (5) _temporary_ (65 70 60 75 66);
array p (5) _temporary_;
do i= 1 to dim(score);
p[i] = (score[i] ge passing[i]);
end;
Passed = sum(of p(*));
drop i;
run;
Of course, all of those array definitions and calculations could be part of the data step that reads the data. Just put in the array and calculations after the INPUT.
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!
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.