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;
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;
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;
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;
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!