## How to show all observations that include special characters

Solved
Frequent Contributor
Posts: 122

# How to show all observations that include special characters

I have one variable in a dataset that include letters, numbers and special characters. Is there way I can screen out observations with ONLY letters, ONLY numbers, and ONLY special characters.

Accepted Solutions
Solution
‎08-26-2014 03:29 AM
Contributor
Posts: 45

## Re: How to show all observations that include special characters

I am presenting some simple code here, You can modify as per your exact requirement!!!

data have;

Input name \$;

datalines;

RAVI

1234

HARI

5678

%\$#@

;

data want;

set have;

if verify(trim(left(name)),'0123456789')=0 then x = 'N';

else if verify(trim(left(name)),'!@#\$%^&*()')=0 then x = 'S';

else x = 'C';

Result :

Obs    name    x

1     RAVI    C

2     1234    N

3     HARI    C

4     5678    N

5     % #     S

All Replies
Solution
‎08-26-2014 03:29 AM
Contributor
Posts: 45

## Re: How to show all observations that include special characters

I am presenting some simple code here, You can modify as per your exact requirement!!!

data have;

Input name \$;

datalines;

RAVI

1234

HARI

5678

%\$#@

;

data want;

set have;

if verify(trim(left(name)),'0123456789')=0 then x = 'N';

else if verify(trim(left(name)),'!@#\$%^&*()')=0 then x = 'S';

else x = 'C';

Result :

Obs    name    x

1     RAVI    C

2     1234    N

3     HARI    C

4     5678    N

5     % #     S

Valued Guide
Posts: 2,191

## Re: How to show all observations that include special characters

See the functions

ANYALPHA

NOTALPHA

ANYDIGIT

NOTDIGIT

ANYCNTRL

NOTCNTRL

Super Contributor
Posts: 319

## Re: How to show all observations that include special characters

Hello,

One solution below:

data have;
length b \$ 7 ;
infile datalines truncover;
input a \$200.;

if length(compress(a,,'ad')) gt 1 then b="Special";
else if length(compress(a,,'d')) gt 1 then b="Letters";
else b="Digits";
datalines;
sdijgh68%#\$kjs
436346
gsgs
#\$%&
eyeryt547&&
GtreUI
;
run;

🔒 This topic is solved and locked.