Search code examples
sassas-iml

Does SAS have a equivalent function to all() or any() in R


In R you can perform a condition across all rows in a column variable by using the all() or any() function. Is there an equivalent method in SAS?

I want condition if ANY rows in column x are negative, this should return TRUE.

Or, if ALL rows in column y are negative, this should return TRUE.

For example

x   y
-1  -2
2   -4
3   -4
4   -3

In R:

  • all(x<0) would give the output FALSE
  • all(y<0) would give the output TRUE

I wish to replicate the same column-wise operation in SAS.


Solution

  • If you want to operate on all observations that might be easiest to do using SQL summary functions.

    SAS will evaluate boolean expressions as 1 for true and 0 for false. So to find out if any observation has a condition you want to test if the MAX( condition ) is true (ie equal to 1). To find out if all observations have the condition you want to test if the MIN( condition ) is true.

    data have ;
      input x y @@;
    cards;
    -1 -2 2 -4 3 -4 4 -3 
    ;
    
    proc sql ;
    create table want as 
      select 
         min(x<0) as ALL_X
       , max(x<0) as ANY_X
       , min(y<0) as ALL_Y
       , max(y<0) as ANY_Y
      from have
    ;
    quit;
    

    Result

    Obs    ALL_X    ANY_X    ALL_Y    ANY_Y
     1       0        1        1        1