SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
kurofufu
Calcite | Level 5

Suppose I have one character variable with length = 1 and I want to check if the value is an alphabet only.

Below is my code.Let me know if you have handier code.

data t2;

  set  t1;

  if var1 in ("A","B","C","D", "E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y", "Z") then ind = 1;

  else ind = 0;

run;

7 REPLIES 7
art297
Opal | Level 21

Do you only want to check for uppercase characters?  Otherwise you could use:

data t2;

  set t1;

  ind=anyalpha(var1);

run;

if you really are only interested in upper case characters, you could use:

data t2;

  set t1;

  ind=anyupper(var1);

run;

Linlin
Lapis Lazuli | Level 10

data t1;
input var1 $;
cards;
a
b
2
5
t
r
;
data t2;

  set  t1;

  ind=ifn(lengthn(compress(var1,,'ka'))=1,1,0);

run;
proc print;run;

GeoffreyBrent
Calcite | Level 5

Another option is using regular expressions:

data t2;

set t1;

one_alpha_rx=prxparse("/[a-zA-Z]/");

ind=prxmatch(one_alpha_rx,x);

drop one_alpha_rx;

run;

For the problem you describe, you'd be better off using Art and Linlin's solutions - they're simpler and probably faster. But if you have to check more complex patterns some time, it's worth learning about regexp matching (this webform won't let me copy and paste, but there's a good paper on this in the SUGI29 archives).

As an example of where regexp comes in handy, I had an application where ID variables were expected to be twelve digits followed by an alpha character and then four more digits. To check whether inputs fit this rule, I used:

legal_pattern=prxparse("/\d{12}[a-zA-Z]\d{4}/");

Note that regexp matching doesn't check whether the variable EXACTLY matches the pattern defined, only whether it appears somewhere in there. But this isn't a problem if the length of the regexp exactly matches the length of the variable.

Amir
PROC Star

Hi,

A similar question was asked about specifying an alphabetic range:

https://communities.sas.com/thread/35517

HTH

Regards,

Amir.

TobyDunn_hotmail_com
Fluorite | Level 6

GeoffreyBrent
Calcite | Level 5

Toby, have you tested that PrxMatch code? When I use that one I'm getting zeroes where they shouldn't be.

TobyDunn_hotmail_com
Fluorite | Level 6

Geoffry,

Opps... I guess I shouldnt try to do too many things at the same time...the pattern modifier should have been a 'i' instead of 'o'.

It should have been:

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 7 replies
  • 18530 views
  • 5 likes
  • 6 in conversation