Search notes:

SAS statements: if

if then

data _null_;
  do i = 1 to 10;

     if i > 5 then put i=;

  end;
run;
Github repository about-SAS, path: /programming/statements/if/then.sas

if then else

data _null_;
  do i = 1 to 9;

     if i > 5 then put i " >  5"; /* Note the semicolon! */
              else put i " <= 5";

  end;
run;
Github repository about-SAS, path: /programming/statements/if/else.sas

if then do

data _null_;

  do i = 1 to 10;

     if i = 5 then do;
         put "------------";
         put "-- i is 5 --";
         put "------------";
     end;

  end;

run;
Github repository about-SAS, path: /programming/statements/if/then-do.sas
See also if … then do … end

Subsetting if

/*  A subsetting if is an "if" without corresponding "then". */

data tq84_one;

  infile datalines;

  length col_2 $10;
  input
    col_1
    col_2
  ;

datalines;
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
;


data tq84_two;
  set tq84_one;

  /*
     While the drop and keep statements control which variables
     make it into the output, the subsetting if determines which
     obserrvations go there.
  */
  if col_2 gt 's';
  
run;

proc print data=tq84_two;
run;
Github repository about-SAS, path: /programming/statements/if/subsetting/simple-example.sas

Missing semicolon

data _null_;
  a = 42;
  if a = 42
     then put 'a=42' /* Note the missing semicolon! */
     else put 'a != 42';
run;
/*
  NOTE: Variable else is uninitialized.
  NOTE: Variable put is uninitialized.
  a=42. . a != 42
  NOTE: DATA statement used (Total process time):
        real time           0.00 seconds
        cpu time            0.00 seconds
*/
Github repository about-SAS, path: /programming/statements/if/missing-semicolon.sas

See also

%if
SAS statements

Index