BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Koke
Fluorite | Level 6

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
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

2 REPLIES 2
Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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
;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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