Table of Contents
The ANYDIGIT function in SAS is a helpful tool for identifying the first numeric character in a given string. It can be utilized in various ways, such as cleaning up data by removing non-numeric characters or extracting specific numbers from a larger string. This function can also be used in conditional statements or data transformations to perform specific actions based on the presence or absence of numeric characters in a string. Overall, the ANYDIGIT function is a valuable asset in data manipulation and analysis tasks in SAS.
Use the ANYDIGIT Function in SAS
You can use the ANYDIGIT function in SAS to return the position of the first digit in a given string.
This function uses the following basic syntax:
ANYDIGIT(expression, [start])
where:
- expression: The string to search
- start (optional): The starting position to search from.
The following example shows how to use this function in practice.
Example: Using the ANYDIGIT Function in SAS
Suppose we have the following dataset in SAS that contains information about various employees at some company:
/*create dataset*/
data my_data;
input employeeID $ sales;
datalines;
54AAF 23
0009A 38
BC18B 40
09H30 12
04429 65
B1300 90
B1700 75
RRHHJ 35
0Y009 40
C6500 23
;
run;
/*view dataset*/
proc printdata=my_data;
We can use the ANYDIGIT function to search for the position of the first digit in the employeeID column:
/*create new dataset*/
data new_data;
set my_data;
firstDigit = anydigit(employeeID);
run;
/*view new dataset*/
proc printdata=new_data;

The new column called firstDigit displays the position of the first occurrence of any digt in the employeeID column.
For example:
- The first digit in 54AAF is in position 1.
- The first digit in 0009A is in position 1.
- The first digit in BC18B is in position 3.
And so on.
We can also use the start argument within the ANYDIGIT function to specify a starting position to search from.
For example, we can use the following code to search for the position of the first digit in the employeeID column starting from position 3:
/*create new dataset*/
data new_data;
set my_data;
firstDigit = anydigit(employeeID, 3);
run;
/*view new dataset*/
proc printdata=new_data;
Notice that employeeID values that only contain an alphabetical character in the first two positions now receive a value of 0 in the firstDigit column since the search for digits now starts at position 3.
The following tutorials explain how to use other common functions in SAS:
Cite this article
stats writer (2024). How can the ANYDIGIT function be used in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-anydigit-function-be-used-in-sas/
stats writer. "How can the ANYDIGIT function be used in SAS?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-anydigit-function-be-used-in-sas/.
stats writer. "How can the ANYDIGIT function be used in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-anydigit-function-be-used-in-sas/.
stats writer (2024) 'How can the ANYDIGIT function be used in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-anydigit-function-be-used-in-sas/.
[1] stats writer, "How can the ANYDIGIT function be used in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the ANYDIGIT function be used in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
