Does SAS have an equivalent function for all () or any () in R

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

I want a 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 instance

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

In R:

  • all(x<0) will give the result FALSE
  • all(y<0) will give the result TRUE

I want to replicate the same operation column by column in SAS.

+4
source share
3 answers

If you want to use all the observations that are easiest to do with SQL summary functions.

SAS 1 true 0 false. , , - , , MAX( condition ) (.. 1). , , , MIN( condition ) .

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;

Obs    ALL_X    ANY_X    ALL_Y    ANY_Y
 1       0        1        1        1

+6

, SAS-IML. , , any all ... loc .

data have ;
  input x y @@;
cards;
1 2 
2 4 
3 -4 
4 -3 
;
run;

proc iml;
    use have;
    read all var {"x" "y"};
    print x y;
    x_all = all(x>0);
    x_any = any(x>0);
    y_all = all(y>0);
    y_any = any(y>0);
    y_pos = y[loc(y>0)];

    print x_all x_any y_all y_any;
    print y_pos;
quit;
+7

SQL, , , - , , SAS, , , , SAS.

data want;
  set have end=eof;
  retain any_x all_x;         *persist the value across rows;
  any_x = max(any_x, (x>0));  *(x>0) 1=true 0=false, keep the MAX (so keep any true);
  all_x = min(all_x, (x>0));  *(x>0) keep the MIN (so keep any false);
  if eof then output;         *only output the final row to a dataset;
  *and/or;
  if eof then do;             *here we output the any/all values to macro variables;
    call symputx('any_x',any_x); *these macro variables can further drive logic;
    call symputx('all_x',all_x); *and exist in a global scope (unless we define otherwise);
  end;
run;
+5

Source: https://habr.com/ru/post/1695084/


All Articles