20.5 C
London
Monday, June 2, 2025
HomeSASData Munging in SASHow to Extract Numbers from String in SAS

How to Extract Numbers from String in SAS

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories