BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cphd
Obsidian | Level 7

Hi all, 

 

I have a phone number variable that has '-' in the middle. I would like to take numbers only. 

 

This is what I want.

 

      Phone               num_phone (what I want)

111-222-3333       1112223333

111-222-4444       1112224444

111-2225555        1112225555

 

 

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input Phone :$20.;
datalines;
111-222-3333
111-222-4444
111-2225555 
;

data want;
    set have;
    NewPhone = compress(phone, '-');
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20
data have;
input Phone :$20.;
datalines;
111-222-3333
111-222-4444
111-2225555 
;

data want;
    set have;
    NewPhone = compress(phone, '-');
run;
ed_sas_member
Meteorite | Level 14

Hi @cphd 

 

Here are two methods to achieve this:

data want;
    set have;
    NewPhone = prxchange('s/-//',-1,phone);
run;
data want;
    set have;
    NewPhone = compress(phone, '-');
run;

Best,

Astounding
PROC Star

Just in case your data contains other special characters such as parentheses, I would use:

NewPhone = compress(phone,,'kd');

 

As a third parameter, KD = Keep Digits

ballardw
Super User

@cphd wrote:

Hi all, 

 

I have a phone number variable that has '-' in the middle. I would like to take numbers only. 

 

This is what I want.

 

      Phone               num_phone (what I want)

111-222-3333       1112223333

111-222-4444       1112224444

111-2225555        1112225555

 

 

Thank you in advance.


By any chance to you have international phone numbers in you data?

 

I worked with a process that did this to "clean" phone numbers and then found that certain international phone numbers with different number construction would fit in the US 10 digit dial string but were now considered to be in US because the first two digits were the country code and the remaining eight were the number within the country.

 

So if you do not know if you have international numbers you might check to see if you have any dialing strings with a dash in the third position.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1633 views
  • 0 likes
  • 5 in conversation