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;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 904 views
  • 0 likes
  • 3 in conversation