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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
RaviKommuri
Fluorite | Level 6

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

View solution in original post

3 REPLIES 3
RaviKommuri
Fluorite | Level 6

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

Peter_C
Rhodochrosite | Level 12

See the functions

ANYALPHA

NOTALPHA

ANYDIGIT

NOTDIGIT

ANYCNTRL

NOTCNTRL

Loko
Barite | Level 11

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 476 views
  • 6 likes
  • 4 in conversation