## DATA Step, Macro, Functions and more

Solved
Occasional Contributor
Posts: 10

[ Edited ]

001 50 70 62 78 85
002 90 86 87 91 94
003 63 72 58 73 68
004 60 68 59 70 65

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

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;```

All Replies
Super User
Posts: 10,211

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
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 10

Solution
‎08-30-2016 12:25 PM
Super User
Posts: 9,599

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

Occasional Contributor
Posts: 10

Incase i do not want to use SUM,? directly
Super User
Posts: 6,754

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

001 50 70 62 78 85
002 90 86 87 91 94
003 63 72 58 73 68
004 60 68 59 70 65

Super User
Posts: 13,508

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.