BookmarkSubscribeRSS Feed
user24
Obsidian | Level 7

How can i remove the data if the variable is not start with a letter. Should i type all the letter or any other short way

 

variable

 

 A153

B124

0564

.1548

na

w853

 

 

I want to keep only these variables. It should be 4 character long

 

variable

A153

B124

w853

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

What about 'na'? That starts with a letter two?

 

Anyway, something like this

 

data have;
input variable $;
datalines;
 A153
B124
0564
.1548
na
w853
;

data want;
   set have;
   where substr(variable,1,1) not in ('0','1','2','3','4','5','6','7','8','9','.',' ');
run;
user24
Obsidian | Level 7
how can i remove if the character long not more than 4 or less than 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Pretty sure your just pulling our leg now, we ask for test data in the form of a datastep every post.  As such this is just logic snippets:

data want;
  string="A153";
  isfirstalpha=ifn(lengthn(compress(char(string,1)," ","a")),0,1);
  islength=lengthn(string);
run;
Astounding
PROC Star

A short way:

 

if length(variable)=4 and anyalpha(variable)=1;

 

By using ANYALPHA, you don't have to guess what characters might arrive in the next batch of data.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just to note its only the first character, so:

if length(variable)=4 and anyalpha(char(variable,1))=1; 

 

Good point about anyalpha() function, saves compress.

Astounding
PROC Star

No, the complications aren't needed. I stand by my original post.

 

ANYALPHA returns the position of the first alpha character.  So by checking that ANYALPHA returns a 1, that's enough.

Ksharp
Super User
data have;
input variable $;
datalines;
 A153
B124
0564
.1548
na
w853
;
data want;
 set have;
 if prxmatch('/^[a-z]\S{3}$/i',strip(variable));
run;
art297
Opal | Level 21

I think@Ksharp meant the following (to ensure that strings were only 4 characters long):

 

data have;
input variable $;
datalines;
 A153
B124xxx
d5555
0564
.1548
na
AAAA
w853
;
data want;
 set have;
 if prxmatch('/^[a-z][a-z0-9]{3}$/i',strip(variable));
run;

Art, CEO, AnalystFinder.com

 

Ksharp
Super User
Yeah.I forgot $ .

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 23731 views
  • 2 likes
  • 7 in conversation