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 90
002 64 64 77 72 71
003 68 69 80 75 70
004 88 77 66 77 67
;
proc
print data = test;
if you could simplified my array that would be appriciate.
here is my log
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
;
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;
before your DO loop.
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
;
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.