Subsetting in SAS – Part 4 (DROP Statement)

Welcome back to Mindful Data Minds! In the last session, we learned about vertical subsetting using the KEEP statement. Now, let’s explore the DROP statement, which is used when you want to remove specific columns from a dataset.

What is DROP?

  • KEEP → Keeps only the columns you specify.
  • DROP → Removes the columns you specify.

Use DROP when you want to delete a few columns and keep the rest.

Watch the Full Tutorial

What You Will Learn

  • How to use DROP to remove specific columns.
  • How to drop variables by name pattern.
  • How to drop all character or numeric variables.
  • How to drop a range of columns using --.
  • How to drop sequentially named columns using -.
  • How to use DROP as a dataset option for efficiency.
  • How to apply DROP when creating new columns or multiple datasets.

Example 1: Drop Specific Columns

Suppose we want to remove Make and Model from SASHELP.CARS:

data d1;
   set sashelp.cars;
   drop make model;
run;

Result: Dataset with 13 columns (from Type to Length).

Example 2: Drop Columns by Name Pattern

You can drop all variables starting with a letter. For example, drop all variables starting with M:

data d2;
   set sashelp.cars;
   drop m:;
run;

Result: Columns like Make, Model, MSRP, MPG_City, MPG_Highway are removed.

Example 3: Drop All Character or Numeric Variables

  • Drop all character variables:
data d3a;
   set sashelp.cars;
   drop _character_;
run;

Result: Dataset with only numeric variables.

  • Drop all numeric variables:
data d3b;
   set sashelp.cars;
   drop _numeric_;
run;

Result: Dataset with only character variables.

Example 4: Drop a Range of Columns

Use double dash (–) to drop columns between two variables:

data d4;
   set sashelp.cars;
   drop make--origin;
run;

Result: Drops Make, Model, Type, and Origin. Keeps the rest.

Example 5: Drop Sequentially Named Columns

If variable names follow a sequence (like Units_1 to Units_24 in SASHELP.APPLIANCE), use a single dash (-):

data d5;
   set sashelp.appliance;
   drop units_1-units_5;
run;

Result: Drops Units_1 through Units_5, keeps Units_6 through Units_24 plus Cycle.

Example 6: Using DROP as a Dataset Option

You can also use DROP as a dataset option for efficiency:

data d6;
   set sashelp.cars(drop=make model);
run;

Result: Drops Make and Model while reading the dataset, saving memory and time.

Example 7: Drop While Creating New Columns

Suppose we create a new column and want to keep only it plus Make:

data d7;
   set sashelp.cars;
   new_col = MSRP * 10;
   drop model--length;
run;

Result: Dataset with only Make and new_col.

Example 8: Different DROP Options for Multiple Outputs

You can apply DROP differently when creating multiple datasets:

data d8(drop=model--length) d9(drop=_character_ new_col);
   set sashelp.cars;
   new_col = MSRP * 10;
run;

Result:

  • d9 → Drops columns from Model to Length.
  • d10 → Drops all character variables plus new_col, keeping only numeric variables.

Next Step

Continue learning by exploring the next tutorial in this series. Also subscribe to get notified about new lessons.

Have a Question?

Drop your doubts in the comments below or contact us.

Leave a Reply

Your email address will not be published. Required fields are marked *