How to Extract Numbers from String in SAS


The easiest way to extract numbers from a string in SAS is to use the COMPRESS 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 SAS documentation page.

Additional Resources

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

How to Normalize Data in SAS
How to Identify Outliers in SAS
How to Create Frequency Tables in SAS

Leave a Reply

Your email address will not be published.