HomeSAS PROGRAMS

Removing dashes and parentheses from phone numbers1 min read

Like Tweet Pin it Share Share Email

Removing dashes and parentheses from phone numbers is one of the common data cleaning activities. This program uses the compress function to remove unwanted characters from a phone number.

data phone_number;
	input Phone $ 1-20;
	Phone_number=compress(Phone, , 'kd');
	datalines;
    (908)235-4490
    (201) 555-77 99
        ;
	title 'Removing dashes and parentheses from phone numbers';

proc print data=phone_number;
run;

The key to this program is the COMPRESS function with the two modifiers ‘k’ (keep) and ‘d’ (digits) as the third argument to the function.

The two modifiers k (keep) and d (digits) are used to keep all the digits in the PHONE value and remove everything else. Note that there are two commas following the first argument. If you used only one comma here, SAS would think you wanted to remove k’s and d’s from the PHONE value.

Removing dashes and parentheses from phone numbers 1

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.