How to Extract Numbers from String in SAS

In SAS, to extract numbers from a string, the function INPUT can be used. The function takes a character string as input and returns a numeric value. The INPUT function also allows for the extraction of specific numbers within a string by specifying the format of the string. For example, if the string contains numbers in a comma-separated format, the format must be specified to extract the correct numbers. This can be done by using the $ sign to specify a comma-separated format. This process can be used to extract any number from a string in SAS.


The easiest way to extract numbers from a string in SAS is to use the function with the ‘A’ modifier.

This function uses the following basic syntax:

data new_data;
    set original_data;
    numbers_only = compress(some_string, '', 'A');
run;

The following example shows how to use this syntax in practice.

Example: Extract Numbers from String in SAS

Suppose we have the following dataset in SAS that shows the names of various college courses:

/*create dataset*/
data original_data;
    input course $12.;
    datalines;
Stats101
Economics203
Business201
Botany411
Calculus101
English201
Chemistry402
Physics102
;
run;

/*view dataset*/
proc print data=original_data;

We can use the following code to extract only the numbers from each course name:

/*extract numbers from course column*/
data new_data;
    set original_data;
    course_number_only = compress(course, '', 'A');
run;

/*view results*/
proc print data=new_data;

Notice that the new column called course_number_only contains only the numbers from the strings in the course column.

If you would instead like to only extract the characters in each string, you can use the COMPRESS function with the ‘d’ modifier instead:

/*extract characters from course column*/
data new_data;
    set original_data;
    course_characters_only = compress(course, '', 'd');
run;

/*view results*/
proc print data=new_data;

Notice that the new column called course_characters_only contains only the numbers from the strings in the course column.

Note: You can find a complete list of modifiers for the COMPRESS function on this .

The following tutorials explain how to perform other common tasks in SAS:

x