How to Delete Rows in SAS (3 Examples)


Here are the three most common ways to delete rows in SAS:

Method 1: Delete Rows Based on One Condition

data new_data;
    set original_data;
    if var1 = "string" then delete;
run;

Method 2: Delete Rows Based on Several Conditions

data new_data;
    set original_data;
    if var1 = "string" and var2 < 10 then delete;
run;

Method 3: Delete Rows Based on One of Several Conditions

data new_data;
    set original_data;
    if var1 = "string" or var2 < 10 then delete;
run;

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data original_data;
    input team $ position $ points;
    datalines;
A Guard 15
A Guard 19
A Guard 22
A Forward 25
A Forward 27
B Guard 11
B Guard 13
B Forward 19
B Forward 22
B Forward 26
;
run;

/*view dataset*/
proc print data=original_data;

Example 1: Delete Rows Based on One Condition

The following code shows how to delete all rows from the dataset where team is equal to “A.”

/*create new dataset*/
data new_data;
    set original_data;
    if team = "A" then delete;
run;

/*view new dataset*/
proc print data=new_data;

Notice that all rows where team was equal to “A” have been deleted.

Example 2: Delete Rows Based on Several Conditions

The following code shows how to delete all rows from the dataset where team is equal to “A” and points is less than 20:

/*create new dataset*/
data new_data;
    set original_data;
    if team = "A" and points < 20 then delete;
run;

/*view new dataset*/
proc print data=new_data;

Notice that the two rows where team was equal to “A” and points was less than 20 have been deleted.

Example 3: Delete Rows Based on One of Several Conditions

The following code shows how to delete all rows from the dataset where team is equal to “A” or points is less than 20:

/*create new dataset*/
data new_data;
    set original_data;
    if team = "A" or points < 20 then delete;
run;

/*view new dataset*/
proc print data=new_data;

Notice that the eight rows where team was equal to “A” or points was less than 20 have been deleted.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Use Proc Summary in SAS
How to Rename Variables in SAS
How to Create New Variables in SAS

Leave a Reply

Your email address will not be published.