BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
frakje
Calcite | Level 5

Hallo once again

 

I have an ID variable called “CPR” which must be 10 characters long. 

Its a char var with numbers. 

 

I want to drop those observations who doesn’t meet this criteria. 

 

My code look like this: 

 

date have; 

set want; 

if lengthn(cpr) <10 then drop;

run; 

 

can anyone help ?

 

kind regards Frank 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Drop is a declarative statement that can't be executed conditionally, as it is meant to alter the data structure.

If you want to get rid of observattons with faulty entries, use a where condition, a subsetting if, or a conditional delete:

/* method with where */
date have; 
set want; 
where lengthn(cpr) ge 10;
run;

/* subsetting if */
date have; 
set want; 
if lengthn(cpr) ge 10;
run;

/* delete */
date have; 
set want; 
if lengthn(cpr) < 10 then delete;
run;

The most performant of these three is the where.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Drop is a declarative statement that can't be executed conditionally, as it is meant to alter the data structure.

If you want to get rid of observattons with faulty entries, use a where condition, a subsetting if, or a conditional delete:

/* method with where */
date have; 
set want; 
where lengthn(cpr) ge 10;
run;

/* subsetting if */
date have; 
set want; 
if lengthn(cpr) ge 10;
run;

/* delete */
date have; 
set want; 
if lengthn(cpr) < 10 then delete;
run;

The most performant of these three is the where.

Astounding
PROC Star

In real life, data mistakes come in all shapes and sizes.  

 

There might be leading blanks.

 

There might be letters mixed in with the numbers.

 

There might be values that are longer than 10 characters.

 

If you want to keep observations that have exactly 10 digits, consider:

 

if length(compress(cpr, , 'kd') ) = 10;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1357 views
  • 0 likes
  • 3 in conversation