Removing dashes and parentheses from phone numbers

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

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

3 thoughts on “Removing dashes and parentheses from phone numbers”

  1. My spouse and I stumbled over here coming from a different website and thought
    I might check things out. I like what I see so now i am following you.
    Look forward to exploring your web page yet again.

    Reply
  2. Hi i am kavin, its my first time to commenting anywhere, when i read this post i thought i could also create comment due to this sensible piece
    of writing.

    Reply

Leave a Reply

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

Share via
Copy link
Powered by Social Snap
%d bloggers like this: