Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Working with SAS Data Sets

Deleting Observations

Use the DELETE statement to mark an observation to be deleted. The general form of the DELETE statement is

DELETE < range > < WHERE(expression) > ;

where
range
specifies a range of observations.

expression
is an expression that is evaluated for being true or false.
The following are examples of valid uses of the DELETE statement.

Code   Action
delete; deletes the current observation
delete point 10; deletes observation 10
delete all where (age>12); deletes all observations where
  AGE is greater than 12

If a file accumulates a number of observations marked as deleted, you can clean out these observations and renumber the remaining observations by using the PURGE statement.

Suppose that the student named John has moved and you want to update the CLASS data set. You can remove the observation using the EDIT and DELETE statements. First, find the observation number of the data for JOHN and store it in the matrix d using the FIND statement. Then submit a DELETE statement to mark the record for deletion. A deleted observation is still physically in the file and still has an observation number, but it is excluded from processing. The deleted observations appear as gaps when you list the file by observation number:

   > find all where(name={'JOHN'}) into d;
   > print d;

                               D
                               5

   > delete point d;

                          1 observation deleted.
   > list all;

           OBS NAME     SEX            AGE    HEIGHT    WEIGHT
        ------ -------- -------- --------- --------- ---------
             1 JOYCE    F          11.0000   51.3000   50.5000
             2 THOMAS   M          11.0000   57.5000   85.0000
             3 JAMES    M          12.0000   57.3000   83.0000
             4 JANE     F          12.0000   59.8000   84.5000
             6 LOUISE   F          12.0000   56.3000   77.0000
             7 ROBERT   M          12.0000   64.8000  128.0000
             8 ALICE    F          13.0000   56.5000   84.0000
             9 BARBARA  F          13.0000   65.3000   98.0000
            10 JEFFREY  M          13.0000   62.5000   84.0000
            11 CAROL    F          14.0000   62.8000  102.5000
            12 HENRY    M          15.0000   63.5000  102.5000
            13 ALFRED   M          14.0000   69.0000  112.5000
            14 JUDY     F          14.0000   64.3000   90.0000
            15 JANET    F          15.0000   62.5000  112.5000
            16 MARY     F          15.0000   66.5000  112.0000
            17 RONALD   M          15.0000   67.0000  133.0000
            18 WILLIAM  M          15.0000   66.5000  112.0000
            19 PHILIP   M          16.0000   72.0000  150.0000
Notice that there is a gap in the data where the deleted observation was (observation 5). To renumber the observations and close the gaps, submit the PURGE statement. Note that the PURGE statement deletes any indexes associated with a data set.
   > purge;

Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Top
Top

Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.