Search code examples
sas

How to delete observations in a SAS Table Using the _n_ variable


I am trying to delete rows in a SAS data set that does not have data in specific rows. Is it possible to do it by including the variable for the Obs column (i.e., the variable n)?

I used the following Data Step, but it did not seem to work (rows 809-868 do not have data, and I am trying to delete those rows):

data two; 
set one; 
if  _n_ >=809 or _n_<=868 then delete; 
run; 

Any input regarding this would be much appreciated!


Solution

  • _n_ is an automatic variable that is set to the index of the implicit loop iteration at the top of the DATA step. It can be used as a proxy for 'row number' but should not be relied for that.

    You can visualize gaps created by deleting according by n by creating a saved variable.

    data want;
      set have;
      myrownumber = _n_;
      if 809 <= _n_ <= 868 then delete;
    run;
    

    Looking at the output you will see a gap in the numbers shown for myrownumber.

    A better approach is define the criteria for your condition does not have data and filter based on that. For a 'does not' mindset there are functions such as N(), NMISS(), CMISS() that can count how many values are present or missing in a row.