DATA Step, Macro, Functions and more

Please help in ARRAY problem

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Please help in ARRAY problem

[ Edited ]

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, 


Accepted Solutions
Solution
‎08-30-2016 12:25 PM
Super User
Super User
Posts: 7,977

Re: Please help in ARRAY problem

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


All Replies
Super User
Posts: 7,832

Re: Please help in ARRAY problem

We can't help without your input dataset.

Please supply the contents of work.score in a data step with cards;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 10

Re: Please help in ARRAY problem

Posted in reply to KurtBremser
Please revisit my question
Solution
‎08-30-2016 12:25 PM
Super User
Super User
Posts: 7,977

Re: Please help in ARRAY problem

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;
Occasional Contributor
Posts: 10

Re: Please help in ARRAY problem

please revisit my question
Occasional Contributor
Posts: 10

Re: Please help in ARRAY problem

Incase i do not want to use SUM,? directly
Super User
Posts: 5,516

Re: Please help in ARRAY problem

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.

Occasional Contributor
Posts: 10

Re: Please help in ARRAY problem

Posted in reply to Astounding

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, 

Super User
Posts: 11,343

Re: Please help in ARRAY problem

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.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 515 views
  • 2 likes
  • 5 in conversation