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

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, 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

8 REPLIES 8
Saurav
Fluorite | Level 6
Please revisit my question
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Saurav
Fluorite | Level 6
please revisit my question
Saurav
Fluorite | Level 6
Incase i do not want to use SUM,? directly
Astounding
PROC Star

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.

Saurav
Fluorite | Level 6

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, 

ballardw
Super User

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.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 2629 views
  • 4 likes
  • 5 in conversation