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
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;
You could use the NOTALPHA function.
Something like this:
if notalpha(myvar)>3 or notalpha(myvar)=0 then ... ;
@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;
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;
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?
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.