BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

If a var is defined as char,

  length x $6.;

How do I check the values of this var that has numbers only?

 

Example, if x has

12346

1a2b3c

1+abc

 

How do I find numeric values such as 12346?

Or is there a way to look for values that's alphabets? special chars?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

Here's one way:

data have;
    input x $6.;
    length x $6.;
    datalines;
12346
1a2b3c
1+abc
abcde
    ;
run;

data string_with_only_numbers;
    set have;
    if anydigit(x) and not anyalpha(x);
run;

data string_with_only_alpha;
    set have;
    if not anydigit(x) and anyalpha(x);
run;

Courtesy of this post: https://communities.sas.com/t5/SAS-Procedures/Check-numeric-values-in-alphanumeric-variables/td-p/19...

View solution in original post

4 REPLIES 4
Reeza
Super User
Take a look at the ANY*/NOT* set of functions that will allow you to scan through, but they operate on each row not a column.

ANYALPHA will check variables for any alphabetical values for example.

Or if you're trying to convert you could just use the ?? options in INPUT

X_num = input(x, ?? 8.);

Which will convert anything to a valid number and suppress any warnings/messages to the log. Bit dangerous to use IMO but also has it's uses.
Tom
Super User Tom
Super User

An easy way to test if the value is a valid representation of a number is to try to convert it to a number.

Just use the normal numeric informat.  Use the ?? modifier to suppress error messages.

data want;
  set have;
  number=input(x,??32.);
  valid_number = not missing(number);
run;

Or if you want to accept strings with commas and dollar signs ( like 1,200 and $5,000) then use the COMMA informat instead.

 

If X can hold strings that are longer than 32 bytes then you might want to also test that the value is not too long.

data want;
  set have;
  if length(strip(x))<=32 then number=input(strip(x),??32.);
  valid_number = not missing(number);
run;

 

mklangley
Lapis Lazuli | Level 10

Here's one way:

data have;
    input x $6.;
    length x $6.;
    datalines;
12346
1a2b3c
1+abc
abcde
    ;
run;

data string_with_only_numbers;
    set have;
    if anydigit(x) and not anyalpha(x);
run;

data string_with_only_alpha;
    set have;
    if not anydigit(x) and anyalpha(x);
run;

Courtesy of this post: https://communities.sas.com/t5/SAS-Procedures/Check-numeric-values-in-alphanumeric-variables/td-p/19...

Ksharp
Super User
data have;
    input x $6.;
    length x $ 6;
flag=ifn(notdigit(strip(x)),0,1);
    datalines;
12346
1a2b3c
1+abc
abcde
;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1167 views
  • 3 likes
  • 5 in conversation