BookmarkSubscribeRSS Feed
serrld113
Obsidian | Level 7

Hi all,

 

I need need help picking up the values where the first 3 characters of the value are between A and Z. I know to do this:

 

substr(myvar,1,3) in ('A',......,'Z')

but don't want to spell out every letter in the alphabet. 

 

Is there a workaround this?

 

Thank you

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

like this?

 

data have;
input string $;
datalines;
abc234
123abc
a2c123
fghfwd
run;

data want;
   set have;
   if prxmatch('/[a-z]{3}/', string);
run;

 

Edit: To ignore cases, add an 'i' like this

 

data have;
input string $;
datalines;
Abc234
123abc
a2c123
fghfwd
run;

data want;
   set have;
   if prxmatch('/[a-z]{3}/i', string);
run;

 

PaigeMiller
Diamond | Level 26

You could use the NOTALPHA function.

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=fedsqlref&docsetTarget=n0xguv...

 

Something like this:

 

if notalpha(myvar)>3 or notalpha(myvar)=0 then ... ;

 

 

--
Paige Miller
novinosrin
Tourmaline | Level 20

@PaigeMiller  You are full of brains. Thank you for that idea

 

data have;
input string $;
datalines;
AbC234
123abc
a2c123
fghfwd
run;

data want;
set have;
if notalpha(substr(string,1,3))=0 then  put  string = 'check for 1st 3 letters in A-Z is true';
run;
novinosrin
Tourmaline | Level 20

easy,safe and fast with anyalpha

 


data have;
input string $;
datalines;
abc234
123abc
a2c123
fghfwd
run;

data want;
set have;
_c=0;
do pos=1 to 3;
_c+anyalpha(string,pos);
end;
if _c=6 then put  string = 'check for 1st 3 letters in A-Z is true';/*write to the log*/
drop _c;
run;
Tom
Super User Tom
Super User

Do you want to test each of the three characters? 

 

Or just that the first letter falls between A and Z?

 

What about lowercase letters?

Ksharp
Super User
data have;
input string $;
datalines;
abc234
123abc
a2c123
fghfwd
Ads23d
BS2Csd
run;

data want;
   set have;
   if prxmatch('/[^a-z]/i', substr(string,1,3)) then delete;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 2983 views
  • 7 likes
  • 6 in conversation