DATA Step, Macro, Functions and more

IF Statements Problem

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

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

00190889295905
00264647772718
003686980757012
004887766776716

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

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
Super User
Posts: 6,502

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
;

 

View solution in original post


All Replies
Super User
Super User
Posts: 6,502

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;

before your DO loop.

Solution
‎07-23-2017 12:15 AM
Super User
Super User
Posts: 6,502

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.

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

Discussion stats
  • 2 replies
  • 384 views
  • 5 likes
  • 2 in conversation