The DELETE statement is used in a DATA step to stop processing the current observation. When DELETE is executed, the current observation is not written to the data sets created by the DATA step, and control immediately returns to the beginning of the DATA step for the next iteration. It is often used in a THEN clause of an IF-THEN statement or in a DO group executed conditionally. Unlike the DROP statement, which excludes variables from an output data set, DELETE excludes entire observations.
Data Analysis
Type : INTERNAL_CREATION
Examples use generated data (datalines).
1 Code Block
DATA STEP Data
Explanation : This example creates a data set 'new_data'. It reads the variables 'id' and 'leafwt'. If the value of 'leafwt' is missing (represented by '.'), the DELETE statement is executed, which removes the entire observation from the final data set. Only observations with a non-missing value for 'leafwt' will be included in 'new_data'.
Copied!
data new_data;
input id leafwt;
datalines;
1 10.5
2 .
3 25.3
4 .
5 18.0
;
if leafwt=. then delete;
run;
proc print data=new_data;
title 'Observations after deleting missing values of LEAFWT';
run;
1
DATA new_data;
2
INPUT id leafwt;
3
DATALINES;
4
110.5
5
2 .
6
325.3
7
4 .
8
518.0
9
;
10
IF leafwt=. THEN delete;
11
RUN;
12
13
PROC PRINTDATA=new_data;
14
title 'Observations after deleting missing values of LEAFWT';
15
RUN;
2 Code Block
DATA STEP Data
Explanation : This example reads in-line raw data (simulating an external file via 'infile datalines'). It creates a 'topsales' data set. For each observation read, it checks if the value of 'yrsales' (annual sales) is less than 100,000. If so, the DELETE statement is executed, excluding that observation from the 'topsales' data set. The final 'topsales' data set will only contain observations where 'yrsales' is greater than or equal to 100,000.
Copied!
data topsales;
infile datalines;
input region $ office $ product $ yrsales;
if yrsales<100000 then delete;
datalines;
East Boston Hardware 120000
West LA Software 95000
North Chicago Electronics 200000
South Miami Services 80000
Central Dallas Retail 150000
;
run;
proc print data=topsales;
title 'Annual Sales Greater Than 100,000';
run;
1
DATA topsales;
2
INFILEDATALINES;
3
INPUT region $ office $ product $ yrsales;
4
IF yrsales<100000THEN delete;
5
DATALINES;
6
East Boston Hardware 120000
7
West LA Software 95000
8
North Chicago Electronics 200000
9
South Miami Services 80000
10
Central Dallas Retail 150000
11
;
12
RUN;
13
14
PROC PRINTDATA=topsales;
15
title 'Annual Sales Greater Than 100,000';
16
RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.