How to check if string is numeric in SAS?

What if you want to find out whether a string has a numerical value? In SAS, you can determine whether a character variable contains any numeric values in several ways.

We have used a flag variable to distinguish between numeric and character values.

In the third method, we will identify character values in the variable, and we will delete them to leave only numeric values.

1. Check if a string is numeric in SAS using the input function and the special “??” format modifier

? or ?? The optional question mark (?) and double question mark (??) format modifiers suppress the printing of both the error messages and the input lines when invalid data values are read.

The ? modifier suppresses the invalid data message.

The ?? modifier also suppresses the invalid data message and, in addition, prevents the automatic variable _ERROR_ from being set to 1 when invalid data are read.

Below is an example of using ?? to determine whether a variable contains non-numeric values or not:

data one;
input Val $;
flag=ifn ((input(val, ?? 8.) eq .), 0, 1);
datalines;
abc.300
300.bef
30.0$bef
50$erf
3456321
abcdefg
123.234
;

footnote if numeric flag=1 else flag=0;
proc print;
run;

How to check if string is numeric in SAS?

The variable FLAG will be set to 1 if the value is numeric including any decimal, otherwise, FLAG will be set to 0.

Note that the input format in the above example is “8.” So only the first 8 bytes of the character string are checked.

Thus, In the 8th observation, Val is “123a5678”, SAS returned the flag as 0 (non-numeric).

In the 9th observation, val=123456789a would return the flag as 1(numeric) as it would only be checking the first 8 bytes of the string.

2. Check if a string is numeric using the combination of LENGTHN and COMPRESS Function.

data one;
	input Val $10.;
	only_char=compress(val, , 'ka');
	only_num=compress(val, , 'kd');
	compress=compress(val,,'ka');
	lengthn=lengthn(compress);
	label compress ="Compress(val)" lengthn ="Lengthn(compress)";
	flag=ifn(lengthn(compress(val, , 'ka')), 0, 1);
	datalines;
 abc.300
300.bef
30.0$bef
50$erf
3456321
abcdefg
123.234
123a5678
12356789a
 ;
footnote if numeric flag=1 else flag=0;
proc print label;
run;

check if string is numeric sas

The key here is to use the compress function with the “ka” option. This will keep only the alphabetic character.
 
In this case, observations 5 and 7, the compress function will return a blank, and the lengthn function will return a zero for a blank character string.

3. Determine if a character variable contains numbers using ANYALPHA and ANYDIGIT function

data one;
input Val $30.;
do i=1 to length(val);
str=substr(val, i, 1);
if not anydigit(str) and anyalpha(str) then
delete;
end;
drop i str;
datalines;
abc.400
300.bef
3456321
abcdefg
123.234
;

proc print;
run;

How to check if string is numeric in SAS?

Within the loop from 1 to length of Val, the SUBSTR function pulls off each byte of Val and evaluates it with ANYALPHA and ANYDIGIT.

When the result of anydigit is 0 and anyalpha is greater than 0, it means there is a non-alphanumeric character is in the value. This observation is deleted and execution returns to the top of the DATA step.

The newly formed data set contains observations containing only numeric characters including any periods.

4. Check if a string is numeric Using the Verify function

If you want to check if a string is numeric in SAS in a simple and fast way: you can use Verify Function, which returns the position of the first character in a string that is not in the search string. If there are no characters in target-expression that are unique from those in search-expression, VERIFY returns a 0.

data one;
input Val $10.;
falg=ifn(verify(strip(val), '.0123456789'), 0, 1);
datalines;
abc.300
300.bef
30.0$bef
50$erf
3456321
abcdefg
123.234
123a5678
12356789a
;

proc print;
run;

How to check if string is numeric in SAS?

So, this was our side of the different ways to check if a string is numeric in SAS. We hoped you might have found it useful.

Moreover, if you have any other suggestions regarding other plagiarism tools, suggest us below the comment section. We would really take those lists in our further blog post.

Thanks for reading!

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap