Skip Navigation

UNC Carolina Population Center

 

Keeping only the observations you want


There are several ways you can let SAS know exactly which observations you want written to a data set. The first example below uses the subsetting IF statement. The second example accomplishes the same thing with the DELETE statement.

The subsetting IF statement

  • IF condition;
    • if condition is true, continue to execute data step
    • if condition is false, stop processing current observation and return to top of data step
    • in particular, if condition is false do not output the current observation being formed in the PDV
       
      Example--only two observations will be output by the data step:

      data alpha;
      infile cards;
      input a b c;
      d=c-b;
      if d>0; /*output obs only if d>0 */
      cards;
      1 5 10
      0 8 7
      1 4 6
      ;
      run;

      proc print;
      run;

      Proc print above would display the data set alpha like this:

      OBS A B C D
      1 1 5 10 5
      2 1 4 6 2


The DELETE statement

  • IF condition THEN DELETE;
    • if condition is true, stop processing current observation and return to top of data step
    • in particular, if condition is true, do not output the current observation being formed in the PDV
       
      Example--this data step gives the same result as the data step above:

      data alpha;
      infile cards;
      input a b c;
      d=c-b;
      if d<=0 then delete; /*do not output obs if d<=0*/
      cards;
      1 5 10
      0 8 7
      1 4 6
      ;
      run;




Another topic?
Questions or comments?  If you are affiliated with the Carolina Population Center, send them to Phil Bardsley.