In SAS, you don't drop "invalid sizes" with one universal command; you drop the rows that contain them. The key condition is deciding what "invalid" means in your data—a negative quantity, an impossible height, a size code outside a known set, or a stored length too short to hold the value. Once that rule is explicit, you can use a WHERE statement, a data step with a subsetting IF, or PROC SQL to remove those records. The right tool depends on whether you're cleaning a disk dataset, filtering during a read, or catching truncation that silently corrupts sizes before any row looks wrong.

What "invalid size" means before you delete anything

The phrase covers at least three different problems in SAS practice, and mixing them up is the fastest way to delete good data. The first is an invalid value: a shoe size of -3, a package dimension of 0, a clothing size of "XXXL" that isn't in your valid list. Here the row exists and the byte content is intact, but the meaning is impossible. The second is an invalid format or informat: you applied a width or read an informat that can't represent the value, so the number lands in the wrong scale or becomes missing. The third is an invalid length or truncation: the variable is defined shorter than the values, SAS keeps the first N characters, and "Extra Large" becomes "Extra La" without an error—just a warning you may never see.

Decide which of these you're facing before you write code, because the fixes differ. Value problems are handled by subsetting rows. Format problems are handled by correcting the FORMAT/INFORMAT or the read logic, then re-reading, not by deleting. Length problems are handled by redefining the variable length during a data step; deleting rows won't recover characters SAS already discarded. If you skip this triage, you may throw away rows that were never the real problem.

A concrete example shows why the distinction matters. A retail extract defines SIZE with $6. but the source contains "XX-Large" (8 characters). On read, SAS stores "XX-Lar" and may log a truncation note depending on how the value entered. If you filter with WHERE SIZE = 'XX-Large', you find nothing and conclude those records don't exist—when in truth they exist but were clipped. No amount of WHERE logic fixes that; you must rebuild the variable at $8. or wider and re-import.

Prerequisites: a defensible validity rule and a backup

Before you drop anything, write down the rule in one sentence you could show a reviewer: "A size is invalid if HEIGHT_CM is missing, negative, or above 250." Vague instincts like "weird sizes" can't be audited and can't be reproduced. Tie each rule to a type, a range, or an allowed-value set.

You also want three safeguards in place. Work on a copy or a library you can restore from, so a mistaken filter isn't permanent. Know your variable types and lengths ahead of time by checking metadata. And confirm how missing values behave in your chosen filter, because in SAS a numeric missing value (.) sorts lower than any number, which changes what comparisons return.

The descriptor portion of a dataset tells you what you actually have rather than what you assume. A quick metadata check shows names, types, and lengths:

If SIZE comes back as type 2 (character) with length 4, you now know that any valid size longer than four characters will have been truncated, and that's a length problem, not a row-validity problem. Confirm this before filtering.

Method 1: Remove invalid rows from a dataset with a data step

The classic approach reads the dataset and writes only the rows that pass your rule using a subsetting IF. This is the most readable method and the easiest to log, because you can count what you removed.

IF here is a subsetting statement, not an assignment, so it keeps only rows where every condition is true. Rows that fail any condition are silently excluded from the output. To see how many you dropped, read the same source twice and compare counts, or use N to track positions:

DELETE removes the current row immediately and returns control to the top of the step, so output never runs for it. The automatic variable N counts iterations of the data step, which is a handy cross-check: total source rows minus N on the final iteration equals rows read, and the message tells you what you filtered.

To verify success, compare row counts from the source and result using PROC SQL or PROC FREQ. A clean audit looks like: source rows from PROC SQL count equals kept rows plus dropped rows, and a PROC FREQ on the cleaned size variable shows no invalid codes remaining.

Method 2: Filter without rewriting using WHERE or PROC SQL

If you only need to analyze or export valid rows and don't need a new physical dataset, a WHERE statement on a PROC is lighter and faster. WHERE is applied before the procedure processes data, and on some engines it can push filtering down to the database.

The same filter in PROC SQL gives you a distinct set of allowed values, which is useful when your "valid size" is really "in this list":

A crucial difference: WHERE cannot reference variables created in the same DATA step, and it cannot use most data-step functions that operate on the program data vector. If your validity test requires computing something first, the data-step IF in Method 1 is the safer route. Conversely, if you have millions of rows on a server, push the filter to the database with WHERE so you never move bad rows across the network.

Note the handling of missing in each syntax. height_cm between 1 and 250 is false when height_cm is missing, because missing numerics do not fall in that range, which quietly removes missing rows—convenient, but be explicit if a reviewer expects to see missing handled. If you want to keep missing as a separate category, test missing(height_cm) = 0 explicitly.

Method 3: Strip invalid size codes from character variables

Character "sizes" need their own treatment because trailing and leading blanks, case, and stray characters all change equality tests. SAS pads character values to the defined length with blanks, so size = 'M' fails if the stored value is 'M ' and the variable is longer. Normalizing before filtering prevents both false matches and false drops.

STRIP removes leading and trailing blanks; UPCASE folds case so "xl" matches "XL." Building a valid set with IN is cleaner than chaining OR conditions and reads like a sentence. If your valid sizes are stored elsewhere—a lookup table of approved codes—merge or use a PROC SQL subquery instead of hardcoding the list, so the rule stays in one place when it changes.

To decide whether a character value is "too short" rather than "wrong," compare the length of the stripped value against expected lengths. A size field that should hold three characters but strips to one is a candidate for review, not necessarily deletion. Dumping the distinct values before and after you clean shows you exactly what got removed:

If a legitimate size appears in neither source nor result, your filter was too aggressive—an example of why you check after every filter rather than batch-deleting on instinct.

Method 4: Fix truncation, the length problem masking as a size problem

If metadata revealed that SIZE was defined at $6. but the source has longer values, deleting rows won't help; you must restore the values. The fix is to re-read the source or reconstruct the width, never to accept the truncated version as valid. When you read raw text with INFILE, assigning a generous length with LENGTH (or an informat wide enough) prevents the clipping in the first place:

TRUNCOVER tells SAS to read whatever is present rather than expecting the full width, which matters when the last field is shorter than its informat. Setting length size $12; before the INPUT ensures no value is clipped to six characters. When the values already live in a dataset truncated to $6., the original characters are gone from that dataset—recover them from the upstream file, not by widening the truncation-free dataset you no longer have.

SAS logs a truncation note for variable-to-variable assignment when, in certain configurations, values exceed the target length; don't assume silence means no truncation. A defensible check is to compare the maximum length of the stored values against the defined length. If the maximum equals the defined length exactly, truncation is plausible and deserves investigation before you trust the filter.

Method 5: Input-level checks that prevent invalid sizes from entering

The most efficient way to drop invalid sizes is to never store them—validate at the point of reading. A data step that reads raw data can flag suspicious records as it loads them, so the invalid rows are identified by the same rule that will later determine validity.

The two named output datasets—shoes_checked for valid and invalid_rows for suspect—turn cleaning into a reviewable artifact. You keep evidence of what was rejected and why, which is invaluable when a downstream colleague asks where certain records went. This is the pattern I recommend for recurring imports: define the rule once, split at ingest, and have a permanent quarantine table.

Validation at input has a limit: it only catches what the rule anticipates. New size codes, unit changes (centimeters in one file, inches in another), and locale formatting will still slip through until the rule is updated. Treat the valid set as configuration you maintain, not a truth written in stone.

Comparing the methods on equal criteria

The methods serve different jobs, so the fair comparison is by purpose, not by vague preference. All figures below assume a moderate dataset on a workstation; runtime on a database or very large file will depend on how much filtering the engine can push down.

Approach Where the rule runs Creates new dataset Catches truncation Best for
Data step IF During read/write of rows Yes No Cleaning and auditing
WHERE on a PROC Before the procedure No No Filtering for reports
PROC SQL In the SQL processor Optional No Set-based valid lists
Character normalization Data step Yes No Mesoy size codes
Length/INFILE redo During ingestion Yes Yes Truncation repair
Reference split During ingestion Yes (two) Partly Recurring imports

Read the table as a decision aid: if you need a clean deliverable file, use a data-step IF; if you just need a report over valid rows, a WHERE avoids writing anything; if the issue is truncated values, only the length and INFILE remedies touch the root cause. No row filter can reverse characters that were never stored.

When filtering is the wrong move and needs a professional

Two conditions should stop you and send the work upstream. First, if the "invalid" sizes encode something legitimate—a separate unit system, a regional size chart, a special-order code—deleting them destroys meaning, and the correct fix is a mapping table maintained by someone who owns the data standard. Second, if the truncation originates in a vendor extract or a production feed, changing how you read it on your machine may violate a contract or mask a defect the source system should fix. In those cases, document the evidence—PROC CONTENTS output, a handful of example records, and counts—and escalate rather than silently patching.

A practical stop rule: once your cleaned output drops more than a small, expected fraction of rows, or once the same records keep reappearing as invalid after repeated imports, stop trying new filters and confirm the definition with the data owner. Persistent reoccurrence usually means the rule itself is wrong, not the data.

A worked example tying the steps together

Suppose a footwear retailer's extract has 50,000 rows, and the business defines a valid record as one where SIZE is one of S, M, L, XL, XXL and QTY is zero or more. Metadata first: PROC CONTENTS shows SIZE stored at $6. and QTY numeric. You check the maximum stored length of SIZE and it equals 6, so some values may be truncated. You re-read the raw file with length size $12;, normalizing with STRIP/UPCASE, and split valid from invalid at ingest. The invalid file contains an entry where SIZE reads "XX-Lar" that only apppears whole after re-reading at the wider length—proof that row-level filtering alone would have missed real orders. After cleaning, PROC FREQ on SIZE shows only the five allowed codes, and the counts of valid plus invalid equal 50,000. That balance check—nothing lost, nothing invented—is the success condition.

Arithmetic you can reproduce: if 1,200 rows were flagged invalid and 48,800 remained, the dropped share is 1,200 ÷ 50,000 × 100 = 2.4 percent. State that figure with its denominator and conditions when you report it, so a reviewer can re-run the same filter and get the same number.

FAQ

Can I drop invalid sizes in place without creating a new dataset? You can overwrite a dataset by writing to the same member, but that's destructive and hard to undo. Write to a new name and replace the original only after your row-count check passes; the safety costs one extra line.

What's the difference between IF and WHERE for dropping rows? IF inside a data step executes per row after the program data vector is built, so it can use computed variables; WHERE is applied earlier by the engine and is faster and pushdown-friendly but cannot reference newly computed variables in the same step.

Why do my lengths or sizes still look wrong after filtering? Because you're probably facing truncation or a format problem, not an invalid value. Deleting rows can't restore discarded characters or fix a wrong informat; check PROC CONTENTS lengths and re-read the source at a sufficient width.

How do I know my filter didn't remove good data? Compare distinct values and counts before and after, and require that valid plus invalid totals equal the source row count. Any unexpected disappearance means the rule is too strict or a normalization step is changing values.