coderX
coderX

Reputation: 474

Check all values in a column

Is there a way to select all values in a column, and then check whether the entire column includes only certain parameters? Here is the code that I have tried, but I can see why it is not doing what I want:

IF City = "8" or
   City = "12" or
   City = "15" or
   City = "24" or
   City = "35"
THEN put "All cities are within New York";

I am trying to select the entire column on 'City', and check to see if that column includes ONLY those 5 values. If it includes ONLY those 5 values, then I want it to print to the log saying that. But, I can see that my method checks each row if it includes one of those, and if even only just 1 row contains one of them, it will print to the log. So I am getting a print to the log for every instance of this.

What I am trying to do is:

want-- IF (all of)City includes 8 & 12 & 15 & 24 & 35
       THEN put "All cities are within New York."

Upvotes: 1

Views: 609

Answers (2)

Haikuo Bian
Haikuo Bian

Reputation: 906

A SQL alternative could be:

proc sql;
select case when sum(not city in ('8','12','15','24','35'))>0 then 'Found city NOT within New York' else 'All cities are within New York' end 
from have;
quit;

Not the same as being shown in the log, and not as efficient as the one @Tom offered.

Upvotes: 0

Tom
Tom

Reputation: 51581

You just need test if any observation is NOT in your list of values.

data _null_;
   if eof then put 'All cities are within New York';
   set have end=eof;
   where not (city in ('8','12','15','24','35') );
   put 'Found city NOT within New York. ' CITY= ;
   stop;
run;

Upvotes: 1

Related Questions