How do I delete rows in SAS? 2

How do I delete rows in SAS?

Deleting rows in SAS refers to the process of removing specific observations from a dataset. This can be done by using the “DATA” or “PROC” statements, along with the “DELETE” or “EXCLUDE” keywords. The rows to be deleted can be specified by using logical expressions or by providing a list of observation numbers. It is important to note that the deleted rows cannot be retrieved, hence caution must be exercised while using this feature. Additionally, it is recommended to create a backup of the dataset before deleting any rows.

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 < 10then 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 printdata=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 printdata=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” andpoints 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 printdata=new_data;

Notice that the two rows where team was equal to “A” andpoints 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 printdata=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:

Cite this article

stats writer (2024). How do I delete rows in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-delete-rows-in-sas/

stats writer. "How do I delete rows in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-i-delete-rows-in-sas/.

stats writer. "How do I delete rows in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-delete-rows-in-sas/.

stats writer (2024) 'How do I delete rows in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-delete-rows-in-sas/.

[1] stats writer, "How do I delete rows in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How do I delete rows in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top