BookmarkSubscribeRSS Feed
mikemangini
Obsidian | Level 7

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?

Thank you for your support.

8 REPLIES 8
ballardw
Super User

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.

data_null__
Jade | Level 19

maybe

e = nmiss(of g

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

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

    ballardw
    Super User

    Arggh, keep forgetting the nmiss!

    Haikuo
    Onyx | Level 15

    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;

    data_null__
    Jade | Level 19

    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

    Haikuo
    Onyx | Level 15

    Oops, forgot checking the log. My bad.

    Ksharp
    Super User

    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;

    KachiM
    Rhodochrosite | Level 12

    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;

    SAS Innovate 2025: Save the Date

     SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

    Save the date!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

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