4 Methods to check if string is numeric in SAS

Are you looking to determine whether a string in your SAS program contains a numerical value? This can be evaluated through various methods in SAS programming.

We’ll begin by discussing the use of an input function, followed by exploring the combination of LENGTHN and COMPRESS functions.

Finally, we’ll delve into a few more techniques, such as the ANYALPHA, ANYDIGIT and VERIFY functions. Each of these methods provides a unique approach to identify numeric values in character variables.

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.

In this article, we demonstrate the use of a flag variable for distinguishing between numeric and character values. This binary unit, usually with a value of 1 for numeric characters and 0 otherwise, is a simple and efficient method for this task. By employing a flag variable, we can ascertain if a string is numeric. It’s noteworthy to understand that such a method doesn’t only add simplicity, but also affords efficiency to the process.

Moving on to the third method for verifying if a SAS string is numeric: this method involves identifying and subsequently eliminating character values within the variable, so only numeric values remain.

The process involves identifying non-numeric characters within the string and deleting them, leaving behind a pure numeric string. For example, assume we have the commands ‘ABC123’; by using this method, we remove ‘ABC’, resulting in ‘123’.

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

Let’s use the ‘??’ format modifier in SAS to determine if a string is numeric. This format modifier originating from the SAS INPUT function plays a crucial role in suppressing non-numeric data.

? 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;
4 Methods 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.

Our next topic takes us to an exploration of how we can ascertain whether a string is numeric or not by employing two functions in unison – LENGTHN and COMPRESS.

Here, the COMPRESS function is called upon to retain alphabetic characters using a specified option, listed as ‘ka’.Let’s take a moment to fractionate a sentiment that may appear muddled.

First, consider a string with mixed contents. How do we determine its numeric identity? This is when invoking the LENGTHN function reveals its worth. Then, the COMPRESS function enters the scene, used with the ‘ka’ option aiding in the retention of alphabetic characters in our string.

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

In the SAS, the ANYALPHA and ANYDIGIT functions serve a particular set of roles. The ANYALPHA function returns the position of the first alphabetic character in a string, whereas ANYDIGIT does the same but for numerical characters.

A vital sub-function that aids these functions is SUBSTR; this function is the workhorse within the loop, scrutinising each byte of the string under observation.

Let’s look at a tangible example to illustrate the use of these functions. We’ll start by examining the first few lines of code; their primary purpose is to initialise the variables and set the foundation for the rest of the code. The mid-section of the code employs the main functions- ANYDIGIT and ANYALPHA, in tandem with SUBSTR.

The final lines of the code determine the outcome based on the string’s composition. When analysing the code output, take note of how the ANYDIgit or ANYALPHA function reacts to specific characters within the string.

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;
4 Methods 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;
4 Methods 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

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

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