Contributor
Posts: 36

Check if array is empty

Is there a way to check to see if the entire contents of an array is empty, and also, to check to see if the entire contents are equal?

Super User
Posts: 13,518

Re: Check if array is empty

How do you define "empty"?

If the array is numeric then

Assume your array is named g

Same = (min(of g(*)) = max(of g(*)));

in a data step will return 1 if all values are the same and 0 if not.

Empty = (same and (min(of g(*))=.)); Empty will be 0 if not empty and 1 if empty.

Posts: 3,852

Re: Check if array is empty

maybe

e = nmiss(of g

• ) eq dim(g);
• or for character array

e = cmiss(of ) eq dim(g);

Super User
Posts: 13,518

Re: Check if array is empty

Arggh, keep forgetting the nmiss!

Posts: 3,167

Re: Check if array is empty

Max/Min should work for char as well for this purpose.

data test;

a1='a1';a2='a1';a3='a1';

output;

a1='a1';a2='a2';a3='a1';

output;

a1='';a2='';a3='';

output;

run;

data want;

length flag_missing flag_equal \$ 10;

set test;

array tst(*) a1-a3;

/*if cmiss(of TST(*))=dim(TST) then flag_missing='YES';*/

IF MISSING (MAX(OF TST(*))) THEN  flag_missing='YES';

ELSE flag_missing='NO';

IF MAX(OF TST(*))= MIN(OF TST(*)) THEN FLAG_EQUAL='YES'; ELSE FLAG_EQUAL='NO';

/*IF REPEAT(TST(1), DIM(TST)-1)=CATS(OF TST(*)) THEN FLAG_EQUAL='YES'; ELSE FLAG_EQUAL='NO';*/

RUN;

Posts: 3,852

Re: Check if array is empty

NOTE: Character values have been converted to numeric values at the places given by: (Line)Column).

45:20   47:11   47:27

NOTE: Invalid numeric data, a1='a1' , at line 45 column 20.

NOTE: Invalid numeric data, a2='a1' , at line 45 column 20.

NOTE: Invalid numeric data, a3='a1' , at line 45 column 20.

NOTE: Invalid numeric data, a1='a1' , at line 47 column 11.

NOTE: Invalid numeric data, a2='a1' , at line 47 column 11.

NOTE: Invalid numeric data, a3='a1' , at line 47 column 11.

NOTE: Invalid numeric data, a1='a1' , at line 47 column 27.

NOTE: Invalid numeric data, a2='a1' , at line 47 column 27.

NOTE: Invalid numeric data, a3='a1' , at line 47 column 27.

flag_missing=YES flag_equal=YES a1=a1 a2=a1 a3=a1 _ERROR_=1 _N_=1

Posts: 3,167

Re: Check if array is empty

Oops, forgot checking the log. My bad.

Super User
Posts: 10,770

Re: Check if array is empty

Are you writing IML code ?  You'd better post it at IML forum . Rick is good at it .

ISEMPTY Function

ISEMPTY(m);

The ISEMPTY function is part of the IMLMLIB library. An empty matrix has no rows or columns. The

ISEMPTY function returns 1 if its argument is an empty matrix; otherwise, the function returns 0 as shown

in the following example:

free x; / * an empty matrix * /

isX = IsEmpty(x);

y = 1:5;

isY = IsEmpty(y);

print isX isY;

Super Contributor
Posts: 324

Re: Check if array is empty

There are SAS functions to check for missing values of an array. There seems to be no function to check the EQUALITY of elements of the array. The last has to be explicitly checked. On matching the first element of the array to the rest of the elements and if found not matching, the comparison can be immediately stopped to declare that elements are not equal. Here is a way with small example. It will also show in which ROW of the data set, the all missing elements are found.

data have;

length S1 S2 S3 \$3;

input X1 - X3 S1 - S3 ;

datalines;

100 200 300 abc yyz yhf

.   .   . xyz ghe mno

200 200 200  .    .   .

100 200 300  xyz xyz xyz

;

run;

data want;

set have;

array n

• _numeric_;
•    array c

• _character_;
•    if nmiss(of n

• ) = dim(n) then Row_N = _N_;
•    if cmiss(of c

• ) = dim(c) then Row_C = _N_ ;
•    if missing(Row_N) then do;

first = n[1];

num_equal = 'T';

do i = 2 to dim(n);

if first ^= n then do; num_equal = 'F'; leave; end;

end;

end;

if missing(Row_C) then do;

char_equal = 'T';

start = c[1];

do i = 2 to dim(c);

if start ^= c then do; char_equal = 'F'; leave; end;

end;

end;

drop first start i;

run;

Discussion stats
• 8 replies
• 1251 views
• 0 likes
• 6 in conversation