## IF Statements Problem

Solved
Occasional Contributor
Posts: 17

# IF Statements Problem

I'm having a problem with stacking when the if the condition is met.

here is my output

Obs id test1 test2 test3 test4 test5 pass 1 2 3 4

 001 90 88 92 95 90 5 002 64 64 77 72 71 8 003 68 69 80 75 70 12 004 88 77 66 77 67 16

the pass value should be 5,3,4,4 repectively

but it turn out that the values are stacking.

here is my code

`data test (drop = i);input id \$ test1 test2 test3 test4 test5;array test (5) test1-test5;do i =1 to 5;if test(i) =test1 and test(i)>= 65 then pass + 1; else if test(i) =test2 and test(i)>= 70 then pass + 1;else if test(i) =test3 and test(i)>= 60 then pass + 1;else if test(i) =test4 and test(i)>= 62 then pass + 1;else if test(i) =test5 and test(i)>= 68 then pass + 1;end;datalines;001 90 88 92 95 90002 64 64 77 72 71003 68 69 80 75 70004 88 77 66 77 67;procprint data = test;`

if you could simplified my array that would be appriciate.

here is my log

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
61
62 data test (drop = i);
63 input id \$ test1 test2 test3 test4 test5
64 ;
65 array test (5) test1-test5;
66 do i =1 to 5;
67 if test(i) =test1 and test(i)>= 65 then pass + 1;
68 else if test(i) =test2 and test(i)>= 70 then pass + 1;
69 else if test(i) =test3 and test(i)>= 60 then pass + 1;
70 else if test(i) =test4 and test(i)>= 62 then pass + 1;
71 else if test(i) =test5 and test(i)>= 68 then pass + 1;
72 else if test(i) ^= test(i) then pass = 0;
73 end;
74 datalines;

NOTE: The data set WORK.TEST has 4 observations and 7 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds

79 ;

80 proc
81 print data = test;
82
83 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
96
Thank you guys

Accepted Solutions
Solution
‎07-23-2017 12:15 AM
Super User
Posts: 8,075

## Re: IF Statements Problem

[ Edited ]

Your logic is wrong. You are testing to find out which cutoff value to use based on the value of the current test instead of the test number. You can simplify the program by making a temporary array to hold the passing level for each test.

``````data test (drop = i);
input id \$ test1-test5;
array test (5) ;
array cutoff (5) _temporary_ (65 70 60 62 68);
do i =1 to 5;
pass=sum(pass,test(I)>=cutoff(I));
end;
datalines;
001 90 88 92 95 90
002 64 64 77 72 71
003 68 69 80 75 70
004 88 77 66 77 67
;``````

All Replies
Super User
Posts: 8,075

## Re: IF Statements Problem

[ Edited ]

You used a SUM statement.

``pass + 1;``

So the value is being retained between observations.  If you didn't want it retained then don't use a sum statement.

Or force it back  to zero by adding

``pass=0;``

Solution
‎07-23-2017 12:15 AM
Super User
Posts: 8,075

## Re: IF Statements Problem

[ Edited ]

Your logic is wrong. You are testing to find out which cutoff value to use based on the value of the current test instead of the test number. You can simplify the program by making a temporary array to hold the passing level for each test.

``````data test (drop = i);
input id \$ test1-test5;
array test (5) ;
array cutoff (5) _temporary_ (65 70 60 62 68);
do i =1 to 5;
pass=sum(pass,test(I)>=cutoff(I));
end;
datalines;
001 90 88 92 95 90
002 64 64 77 72 71
003 68 69 80 75 70
004 88 77 66 77 67
;``````

☑ This topic is solved.