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

Hi, 

I have the following code that i would like to turn into a loop.:

 

data testset;
set test;
if Test1 >= 65 
	then Passed1=1;
	else Passed1=0;
if Test2 >= 70 
	then Passed2=1;
	else Passed2=0;
if Test3 >= 60
	then Passed3=1;
	else Passed3=0;
if Test4 >= 62
	then Passed4=1;
	else Passed4=0;
if Test5 >= 68
	then Passed5=1;
	else Passed5=0;
output;
run;

So far, i was able to come up with :

data testset (drop=i);
set test;
ARRAY Test{5} Test1-Test5;
ARRAY Passed{5};
DO i=1 TO 5;
IF Test{1} >= 65 OR Test{2} >= 70 OR Test{3} >= 60 OR Test{4} >= 62 OR Test{5} >= 68
THEN Passed{i}=1;
ELSE Passed{i}=0;
END;
RUN;

What I want it todo is to put the respective 'passed' as 1 if it meets the criteria and 0 if it doesn't. Each group has a different criteria to be met but the code i wrote doesnt do that. I know its because i used "OR" but what would I use to fix it so that it gives me the output i want for each individual group?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

To turn this into a loop you would need a third array holding the cutoffs.  I'm not sure this is any clearer than  your original program, but here is the idea:

data testset (drop=i);
set test;
ARRAY Test{5} Test1-Test5;
ARRAY Passed{5};
array cutoffs {5} _temporary_ (65 70 60 62 68);
do i=1 to 5;
  if test{i} >= cutoffs{i} then Passed{i}=1;
   else Passed{i}=0;
end;
run;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

To turn this into a loop you would need a third array holding the cutoffs.  I'm not sure this is any clearer than  your original program, but here is the idea:

data testset (drop=i);
set test;
ARRAY Test{5} Test1-Test5;
ARRAY Passed{5};
array cutoffs {5} _temporary_ (65 70 60 62 68);
do i=1 to 5;
  if test{i} >= cutoffs{i} then Passed{i}=1;
   else Passed{i}=0;
end;
run;
SASUsr007
Calcite | Level 5
That is perfect! Thank you so much.
ballardw
Super User

It might help to describe how you intend to use this.

It could be than your additional variables are not even needed and a format would suffice.

 

proc format;

value test1_
low -<65 = '0'
65-high = '1'
;
value test2_
low -<70 = '0'
70-high = '1'
;

data example;
  input test1 test2;
datalines;
45   50
64.9 69.9
65   70
99   81
;

proc print data=example;
   format test1 test1_. test2 test2_.;
run;

The formatted values would be used by report, most analysis and graphing procedures.

If your request for a "loop" was because you have a large number of "test" values to recode the format may be more efficient if some (or many or most) of the test values have identical ranges. Just assign the same format to multiple variables.

 

Plus if at a later date it is determined that the cutoff for test2 should be 69.5 instead of 70 you only need to change the code in the format. There would be no need to rebuild the data set again just run the report, analysis or graphs using the new format definition assigned.

 

There is a restriction on format names that they not end in a number hence the _ in the names.

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 3 replies
  • 534 views
  • 2 likes
  • 3 in conversation